public class Solution {
public int count = 0;
public int movingCount(int threshold, int rows, int cols)
{
boolean[] visited = new boolean[rows*cols];
for(int k=0;k<rows*cols;k++)
visited[k] = false;
move(threshold,rows,cols,visited,0,0);
return count;
}
public void move(int threshold, int rows, int cols,boolean[] visited,int i,int j)
{
//如果该点坐标没越界且没被访问过
if( (i>-1) && (i<rows) && (j>-1) && (j<cols) && (visited[i*cols+j]==false) )
{
if(check(i,j)<=threshold)
{
visited[i*cols+j] = true;
count++;
//向下、向右移动
move(threshold,rows,cols,visited,i,j+1);
move(threshold,rows,cols,visited,i+1,j);
}
}
}
//计算坐标各位之和
public int check(int x,int y)
{
int sum = 0;
while(x!=0)
{
sum = sum + x%10;
x = x/10;
}
while(y!=0)
{
sum = sum + y%10;
y = y/10;
}
return sum;
}
}
13、机器人的运动范围
最新推荐文章于 2023-10-12 16:14:35 发布