Java | BFS soln
The main intution of the question is to store the cordinates of tail of the snake and position and travel according to it.\nAs we have to find the minimum setps
The main intution of the question is to store the cordinates of tail of the snake and position and travel according to it.\nAs we have to find the minimum setps BFS is the first thing that comes to mind.\nSo here is its implementaion - \n```\nclass Solution {\n    \n    public class Pair{\n        int x;\n        int y;\n        int pos;\n        int st;\n        \n        Pair(int x, int y, int pos, int st){\n            this.x = x;\n            this.y = y;\n            this.pos = pos;\n            this.st = st;\n        }\n    }\n    \n    public void Add_Neighbours(int[][] grid, Queue<Pair> Q, Pair rem){\n        \n        int n = grid.length;\n        int pos = rem.pos;\n        int x = rem.x;\n        int y = rem.y;\n        \n        if(pos == 0){\n            if(y + 2 < n && grid[x][y + 2] == 0){\n                Q.add(new Pair(x , y + 1, pos, rem.st + 1));\n            }\n            if(x + 1 < n && grid[x + 1][y]  == 0 && grid[x + 1][y + 1] == 0){\n                Q.add(new Pair(x + 1, y, pos, rem.st + 1));\n                Q.add(new Pair(x, y, 1, rem.st + 1));\n            }\n        }\n        else{\n            if(y + 1 < n && grid[x][y + 1] == 0 && grid[x + 1][y + 1] == 0){\n                Q.add(new Pair(x, y + 1, pos, rem.st + 1));\n                Q.add(new Pair(x, y, 0, rem.st + 1));\n            }\n            \n            if(x + 2 < n && grid[x + 2][y] == 0){\n                Q.add(new Pair(x + 1, y, pos, rem.st + 1));\n            }\n        }\n    }\n    \n    public int minimumMoves(int[][] grid) {\n        \n        int n = grid.length;\n        \n        Queue<Pair> Q = new LinkedList();\n        boolean[][][] visited = new boolean[n][n][2];\n        \n        Q.add(new Pair(0, 0, 0, 0));\n        \n        while(Q.size() > 0){    \n            Pair rem = Q.remove();\n            \n            if(visited[rem.x][rem.y][rem.pos] == true)   continue;\n            \n            visited[rem.x][rem.y][rem.pos] = true;\n            \n            if(rem.x == n - 1 && rem.y == n - 2 && rem.pos == 0)    return rem.st;\n            \n            Add_Neighbours(grid, Q, rem);\n        }\n        \n        return -1;\n    }\n}\n```
['Breadth-First Search', 'Java']