Leetcode Interview Question 13. The range of motion of the robot

Title description

A ground checkered m rows and n columns, from the coordinates of [0,0]the coordinate [m-1,n-1]. A robot [0, 0]starts to move from a grid of coordinates . It can move one grid to the left, right, up, and down at a time (it cannot move beyond the grid), nor can it enter a grid where the sum of the row and column coordinates is greater than k.

For example, when k is 18, the robot can enter the square [35, 37] because 3 + 5 + 3 + 7 = 18. But it cannot enter the square [35, 38] because 3 + 5 + 3 + 8 = 19. How many grids can the robot reach?

Example 1:

Input: m = 2, n = 3, k = 1
Output: 3

Example 2:

Input: m = 3, n = 1, k = 0
Output: 1

answer

From the point of view, it is a traditional conditional search question. It is easy to think of DFS or BFS. The goal of both is to traverse the entire matrix, the difference is that the search order is different. DFS is to go one direction, go back one step, go to the end, then go back, and so on (that is, the result of the stack); BFS is to search forward row by row.

From the point of view of optimization, although it can be up, down, left, and right, but because the starting point is at [0,0], it only needs to consider the downward and right.

DFS(java)

DFS

class Solution {
    int max = 0;
    public boolean into(int a, int b, int k) {
        int count = 0;
        while (a > 0) {
            count += a % 10;
            a = a / 10;
        }
        while (b > 0) {
            count += b % 10;
            b = b / 10;
        }
        if (count <= k) {
            return true;
        } else {
            return false;
        }
    }
    public void step(int a, int b, int m, int n, int k, int[][] flag) {
        if (flag[a][b] == 1) return;
        //判断右侧有无格子且根据k能否进入
        if (a + 1 < m && into(a + 1 ,b ,k)) {
            step(a + 1, b, m, n, k, flag);  
        } 
        //判断下侧有无格子且根据k能否进入
        if (b + 1 < n && into(a, b + 1,k)) {
                step(a, b + 1, m, n, k, flag);
        }
        flag[a][b] = 1;
        max += 1;
        
    }
    public int movingCount(int m, int n, int k) {
        int[][] flag = new int [m][n];
        step(0, 0, m, n, k, flag);
        return max;
    }
}

Complexity analysis

  • time complexity: O ( n m ) O(n*m)
  • Space complexity: O ( n m ) O(n*m)

DFS2 (java)

The same is DFS, but the writing method is obviously optimized a lot.

class Solution {
    public int movingCount(int m, int n, int k) {
        //设置辅助数组
        boolean[][] visited = new boolean[m][n];
        //返回结果
        return dfs(0, 0, m, n, k, visited);
    }
    public int dfs(int i, int j, int m, int n, int k, boolean[][] visited) {
        //把所有限制条件用||来连接起来
        if(i >= m || j >= n || k < getSum(i) + getSum(j) || visited[i][j]) {
            return 0;
        }
        visited[i][j] = true;
        // 每次返回值加一
        return 1 + dfs(i + 1, j, m, n, k, visited) + dfs(i, j + 1, m, n, k, visited);
    }
    
    private int getSum(int a) {
        int sum = a % 10;
        int tmp = a / 10;
        while(tmp > 0) {
            sum += tmp % 10;
            tmp /= 10;
        }
        return sum;
    }
}
  • time complexity: O ( n m ) O(n * m)
  • Space complexity: O ( n m ) O(n * m)

FSO (java)

Paste the bfs template to traverse the space in a first-in first-out manner.

while queue 不空:
    cur = queue.pop()
    for 节点 in cur的所有相邻节点:
        if 该节点有效且未访问过:
            queue.push(该节点)

According to this idea, the writing is as follows

class Solution {
    //计算数位之和
    private int getSum(int a) {
        int count = 0;
        while (a > 0) {
            count += a % 10;
            a = a / 10;
        }
        return count;
    }
    public int movingCount(int m, int n, int k) {
        // 辅助数组,默认值为false。(同int[][],默认为0)
        boolean[][] visited = new boolean[m][n];
        int res = 0;

        //BFS
        Queue<int[]> queue= new LinkedList<int[]>();

        // 初始化,同new int[4]
        queue.add(new int[] { 0, 0, 0, 0 });

        while(queue.size() > 0) {
            int[] x = queue.poll();
            int i = x[0], j = x[1], si = x[2], sj = x[3];
            if(i >= m || j >= n || k < si + sj || visited[i][j]) continue;
            visited[i][j] = true;
            res ++;
            //添加相邻位置
            queue.add(new int[] { i + 1, j, getSum(i + 1), getSum(j) });
            queue.add(new int[] { i, j + 1, getSum(i), getSum(j + 1) });
        }
        return res;
    }
}
  • time complexity: O ( n m ) O(n * m)
  • Space complexity: O ( n m ) O(n * m)
Published 50 original articles · praised 51 · visits 1972

Guess you like

Origin blog.csdn.net/Chen_2018k/article/details/105400338