BFS easy code
Approach\n Describe your approach to solving the problem. \nThis code checks if (i, j) is reachable. If so, it adds the position to the queue. I used depth to m
# Approach\n<!-- Describe your approach to solving the problem. -->\nThis code checks if (i, j) is reachable. If so, it adds the position to the queue. I used depth to monitor the current column, and the maximum depth indicates the maximum number of moves possible (starting from column 0).\n[HackMD](https://hackmd.io/@Aji-Hsu/B1ZgaLAeJx)\n\n# Complexity\n- Time complexity: $$O(m * n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(m * n)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```java []\nimport java.util.LinkedList;\nclass Solution {\n    public int maxMoves(int[][] grid) {\n        int m = grid.length;\n        int n = grid[0].length;\n\n        int[] diri = { 1, 0, -1 };\n        int[] dirj = { 1, 1, 1 };\n        boolean[] visit = new boolean[m];\n\n        LinkedList<Pair> Q = new LinkedList<>();\n        for (int i = 0; i < m; i++) Q.offerLast(new Pair(i, 0));\n\n        int depth = 0;\n        while (!Q.isEmpty()) { // BFS\n            Pair p = Q.pollFirst();\n\n            if (p.j != depth) {\n                depth = p.j;\n                visit = new boolean[m];\n            }\n            for (int k = 0; k < 3; k++) {\n                int ii = p.i + diri[k];\n                int jj = p.j + dirj[k];\n\n                if (ii < 0 || ii >= m || jj < 0 || jj >= n) continue;\n                else if (grid[ii][jj] > grid[p.i][p.j] && !visit[ii]) {\n                    visit[ii] = true;\n                    Q.offerLast(new Pair(ii, jj));\n                }\n            }\n        }\n\n        return depth;\n    }\n}\n\nclass Pair {\n    int i, j;\n    Pair (int i, int j) {\n        this.i = i;\n        this.j = j;\n    }\n}\n```
['Java']