# 机器人的运动范围 java

public class Solution {
public int movingCount(int threshold, int rows, int cols)
{
if (threshold < 0) {
return 0;
}
boolean[][] tag = new boolean[rows][cols];
Node21 node = new Node21(0, 0);
nodeList.push(node);
int sum = 1;
tag[0][0] = true;
while (!nodeList.isEmpty()) {
Node21 nowNode = nodeList.pop();
int[] dx = {1, -1, 0, 0};
int[] dy = {0, 0, 1, -1};
for (int i = 0; i < 4; ++i) {
Node21 tempNode = new Node21(nowNode.x + dx[i], nowNode.y + dy[i]);
if (judge(tempNode, tag, threshold)) {
++sum;
nodeList.push(tempNode);
}
}
}
return sum;
}
public boolean judge(Node21 tempNode, boolean[][] tag, int limitSum) {
int sum = 0, tempX = tempNode.x, tempY = tempNode.y;
while (tempX != 0) {
sum = sum + tempX % 10;
tempX = tempX / 10;
}
while (tempY != 0) {
sum = sum + tempY % 10;
tempY = tempY / 10;
}
if (sum <= limitSum) {
if (tempNode.x < 0 || tempNode.x >= tag.length || tempNode.y < 0 || tempNode.y >= tag[0].length) {
return false;
}
if (tag[tempNode.x][tempNode.y] == false) {
tag[tempNode.x][tempNode.y] = true;
return true;
} else {
return false;
}
} else {
return false;
}

}
}
class Node21 {
int x, y;
public Node21(int x, int y) {
this.x = x;
this.y = y;
}
}