C++| BFS
\nclass Solution {\npublic:\n    int minimumMoves(vector<vector<int>>& nums) {\n        map<tuple<int,int,int>,int> mp;  // tuple stores 3 parameter\n          
```\nclass Solution {\npublic:\n    int minimumMoves(vector<vector<int>>& nums) {\n        map<tuple<int,int,int>,int> mp;  // tuple stores 3 parameter\n                                         // {row of head of snake, col of head of snake, alignment of snake}\n\t\t\t\t\t\t\t\t\t\t // alignment = 0 mean horizontal, alignment = 1 means vertical\n        queue<vector<int>> q;  //stores {row of head of snake, col of head of snake, alignment of snake}\n        q.push({0,1,0}); mp[{0,1,0}]++;\n        int l = 0,n=nums.size();\n        while(!q.empty()){\n            int s = q.size();\n            while(s--){\n                vector<int> t = q.front(); q.pop();\n                int i = t[0], j =t[1];\n                if(i==n-1 && j==n-1 && t[2]==0) return l; // check the destination, and return currenet level if found\n                if(t[2]==0){  // if direction is horizontal\n                    if(j+1<n && nums[i][j+1]==0 && mp.find({i,j+1,0})==mp.end()){ // check if it can move to right \n                        mp[{i,j+1,0}]++; q.push({i,j+1,0});\n                    }\n                    if(i+1<n && nums[i+1][j]==0 && nums[i+1][j-1]==0){\n                        if(mp.find({i+1,j,0})==mp.end()){  // check if it can move to down \n                           mp[{i+1,j,0}]++; q.push({i+1,j,0}); \n                        }\n                        if(mp.find({i+1,j-1,1})==mp.end()){   // check if it can rotate clockwise \n                            mp[{i+1,j-1,1}]++; q.push({i+1,j-1,1}); \n                        }\n                    }\n                }\n                else{    // if direction is vertical \n                    if(i+1<n && nums[i+1][j]==0 && mp.find({i+1,j,1})==mp.end()) {  // check if it can move to down \n                        mp[{i+1,j,1}]++; q.push({i+1,j,1});\n                    }\n                    if(j+1<n && nums[i][j+1]==0 && nums[i-1][j+1]==0){\n                        if(mp.find({i,j+1,1})==mp.end()){   // check if it can move to right \n                            mp[{i,j+1,1}]++; q.push({i,j+1,1});\n                        }\n                        if(mp.find({i-1,j+1,0})==mp.end()){   // check if it can rotate anticlockwise\n                            mp[{i-1,j+1,0}]++; q.push({i-1,j+1,0});\n                        }\n                    } \n                }\n            }\n            l++;\n        }\n        return -1;\n    }\n};\n```\n**Upvote if it helps**
['C', 'Shortest Path']