1210. Minimum Moves to Reach Target with Rotations.cpp
Code\n\nclass Solution {\n   bool isValid(int tr, int tc, int hr, int hc, vector<vector<int>>& grid)\n   {\n      int n = grid.size();\n     return tr < n && tc
**Code**\n```\nclass Solution {\n   bool isValid(int tr, int tc, int hr, int hc, vector<vector<int>>& grid)\n   {\n      int n = grid.size();\n     return tr < n && tc < n && hr < n && hc < n && grid[tr][tc] == 0 && grid[hr][hc] == 0;\n   }\n public:\n    int minimumMoves(vector<vector<int>>& grid) {\n        unordered_map<string,bool> vis;\n        int n = grid.size(), moves = 0;\n        queue<vector<int>> q;\n        q.push({0,0,1});\n        while(!q.empty())\n        {\n           int size = q.size();\n           while(size--)\n           {\n               auto curr = q.front(); \n                q.pop();\n              int tr = curr[0], tc = curr[1], isHorizontal = curr[2], hr, hc;\n              string state = to_string(tr) + \'$\' + to_string(tc) + \'$\' + to_string(isHorizontal);\n              if(vis[state]) continue; \n              vis[state] = true;\n              if(tr == n-1 && tc == n-2 && isHorizontal) \n               return moves;\n               int newTr,newTc,newHr,newHc;\n                if(isHorizontal == 1)\n                {\n                newTr = tr, newTc = tc, newHr = tr, newHc = tc + 2;\n                if(isValid(newTr,newTc,newHr,newHc,grid)) \n                  q.push({tr,tc+1,1});\n                newTr = tr+1, newTc = tc, newHr = tr+1, newHc = tc+1;\n                if(isValid(newTr,newTc,newHr,newHc,grid)) \n                 q.push({tr+1,tc,1});\n                newTr = tr, newTc = tc, newHr = tr+1, newHc = tc;\n                if(isValid(newTr,newTc,newHr,newHc,grid) && grid[tr+1][tc+1] == 0) \n                   q.push({tr,tc,0}); \n                }\n               else\n               {\n                newTr = tr, newTc = tc+1, newHr = tr+1, newHc = tc+1;\n                if(isValid(newTr,newTc,newHr,newHc,grid)) \n                 q.push({tr,tc+1,0}); \n                newTr = tr+1, newTc = tc, newHr = tr+2, newHc = tc;\n                if(isValid(newTr,newTc,newHr,newHc,grid)) \n                 q.push({tr+1,tc,0});\n                newTr = tr, newTc = tc, newHr = tr, newHc = tc+1;\n                if(isValid(newTr,newTc,newHr,newHc,grid) && grid[tr+1][tc+1] == 0)  \n                   q.push({tr,tc,1});  \n               }    \n            }\n         moves++;\n        }\n    return -1;\n    }\n};\n```
['C']