leetcode 机器人能到达的位置

原题点这里

bfs可以实现。这里注意的是,机器人从00出发,我们只要向右,向下走就可以了

public static int movingCount(int m, int n, int k) {
        int[] dx=new int[] {-1,1,0,0};
        int[] dy = new int[]{0,0,-1,1};
        boolean[][] vis = new boolean[m][n];
        Queue<int[]> q = new LinkedList<>();
        q.offer(new int[]{0,0});
        vis[0][0]=true;
        int ans = 1;
        while (!q.isEmpty()){
            int[] nowN = q.poll();
            for(int i=0;i<4;i++){

                int nextX = nowN[0]+dx[i];
                int nextY = nowN[1]+dy[i];
                //boolean flag = canMove(nextX,nextY,k);
                if(nextX>=0&&nextX<m &&nextY>=0&&nextY<n&&!vis[nextX][nextY]&&canMove(nextX,nextY,k)){
                    ans++;
                    vis[nextX][nextY]=true;
                    q.offer(new int[]{nextX,nextY});
                }


            }
        }
        return ans;

    }
private static boolean canMove(int nextX, int nextY,int k) {
        int sum=0;
        while (nextX>0){
            sum+=nextX%10;
            nextX/=10;
        }
        while (nextY>0){
            sum+=nextY%10;
            nextY/=10;
        }
        return sum<=k ? true:false;

    }