原题链接:JZ13 机器人的运动范围
class Solution {
public:
bool fun(int x,int y,int s)
{
int sum=0;
while(x)
{
sum+=x%10;x/=10;
}
while(y)
{
sum+=y%10;y/=10;
}
if(sum<=s)
return true;
else
return false;
}
int ans=0;
int v[110][110];
void dfs(int x,int y,int threshold, int rows, int cols)
{
if(v[x][y])
return ;
if(fun(x,y,threshold) && v[x][y]==0)
{
ans++;v[x][y]=1;
}
else
return ;
if(x-1>=0) dfs(x-1,y,threshold,rows,cols);//上
if(x+1<=rows-1) dfs(x+1,y,threshold,rows,cols);//下
if(y-1>=0) dfs(x,y-1,threshold,rows,cols);//上
if(y+1<=cols-1) dfs(x,y+1,threshold,rows,cols);//下
}
int movingCount(int threshold, int rows, int cols) {
dfs(0,0,threshold,rows,cols);
return ans;
}
};