class Solution {
public int movingCount(int m, int n, int k) {
boolean[][] vis = new boolean[m][n];
int res = 0;
Queue<int[]> queue = new LinkedList<>();
queue.offer(new int[]{0, 0});
vis[0][0] = true;
while(!queue.isEmpty()) {
int[] temp = queue.poll();
int x = temp[0];
int y = temp[1];
if(getTwoSum(x, y) > k) continue;
res++;
int[] dx = {0, 0, 1, -1};
int[] dy = {1, -1, 0, 0};
for(int i = 0; i < 4; i++) {
int a = x + dx[i];
int b = y + dy[i];
if(a >= 0 && a < m && b >=0 && b < n && vis[a][b] == false && getTwoSum(a, b) <= k) {
queue.offer(new int[]{a, b});
vis[a][b] = true;
}
}
}
return res;
}
public int getSum(int x) {
int res = 0;
while(x != 0) {
res += x % 10;
x /= 10;
}
return res;
}
public int getTwoSum(int x, int y) {
return getSum(x) + getSum(y);
}
}
02-22
860
04-20
120