当前位置:网站首页>Sword finger offer 13 Range of motion of robot

Sword finger offer 13 Range of motion of robot

2022-06-24 22:55:00 Who knows the big dream I

There is one on the ground m That's ok n Grid of columns , From coordinates [0,0] To coordinates [m-1,n-1] . A robot from coordinates [0, 0] The grid starts to move , It can go left every time 、 Right 、 On 、 Move down one space ( Can't move out of the box ), The sum of the digits of row coordinates and column coordinates is greater than k Lattice of . for example , When k by 18 when , Robots can enter the grid [35, 37] , because 3+5+3+7=18. But it can't get into the grid [35, 38], because 3+5+3+8=19. How many squares 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
Tips

1 <= n,m <= 100
0 <= k <= 20

class Solution {
    
     int res=0;
    public int movingCount(int m, int n, int k) {
    
       boolean[][] arr = new boolean[m][n];// Used as a tag array 
        dfs(0,0,m,n,k,arr);
        return res;
    }
      private void dfs(int i, int j, int m, int n,int k,boolean[][] arr) {
    
        // Basic judgment + Judge whether to walk this road 
        if (i>=m||i<0||j>=n||j<0||arr[i][j]){
    
            return;
        }
        //m No, I haven't . Mark... First , In judging whether it conforms to the meaning of the question 
        arr[i][j]=true;
        // Sum up 
        int sum =i%10+j%10+i/10+j/10;
        // Judge whether the conditions are met 
        if (sum>k) return;
        res++;
        // Direct large-scale casting 
        dfs(i+1,j,m,n,k,arr);
        dfs(i-1,j,m,n,k,arr);
        dfs(i,j+1,m,n,k,arr);
        dfs(i,j-1,m,n,k,arr);

    }
}
原网站

版权声明
本文为[Who knows the big dream I]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206241707077757.html