题解 | #机器人的运动范围之动态规划#
机器人的运动范围
http://www.nowcoder.com/practice/6e5207314b5241fb83f2329e89fdecc8
创新型题解~
public class Solution {
public int movingCount(int threshold, int rows, int cols) {
int ans = 0;
boolean[][] map = new boolean[rows][cols];
map[0][0] = true;
for(int i = 0; i < rows ; i++){
for(int j = 0; j < cols ; j++){
int temp = (i)/10+(i)%10+(j)/10+(j)%10;
if(temp<=threshold){
if(i-1>=0&&map[i-1][j]){
map[i][j] = true;
}else if(i+1<rows&&map[i+1][j]){
map[i][j] = true;
}else if(j-1>=0&&map[i][j-1]){
map[i][j] = true;
}else if(j+1<cols&&map[i][j+1]){
map[i][j] = true;
}
}
}
}
for(int i = 0; i < rows; i++){
for(int j = 0; j < cols; j++){
if(map[i][j]){
ans++;
}
}
}
return ans;
}
}