1、问题
2、算法标签
1、DFS
3、通过代码
class Solution {
public:
set<pair<int,int> > ans;
int flag[55][55];
bool check(int threshold, int rows, int cols){
int temp=0;
while(rows>0){
temp+=rows%10;
rows/=10;
}
while(cols>0){
temp+=cols%10;
cols/=10;
}
if(temp>threshold){
return false;
}
else{
return true;
}
}
void dfs(int threshold, int rows, int cols,int x,int y){
flag[x][y]=1;
ans.insert(make_pair(x,y));
if(x-1>=0&&flag[x-1][y]==0){
if(check(threshold,x-1,y)){
dfs(threshold,rows,cols,x-1,y);
}
else{
flag[x-1][y]=1;
}
}
if(y-1>=0&&flag[x][y-1]==0){
if(check(threshold,x,y-1)){
dfs(threshold,rows,cols,x,y-1);
}
else{
flag[x][y-1]=1;
}
}
if(x+1<rows&&flag[x+1][y]==0){
if(check(threshold,x+1,y)){
dfs(threshold,rows,cols,x+1,y);
}
else{
flag[x+1][y]=1;
}
}
if(y+1<cols&&flag[x][y+1]==0){
if(check(threshold,x,y+1)){
dfs(threshold,rows,cols,x,y+1);
}
else{
flag[x][y+1]=1;
}
}
}
int movingCount(int threshold, int rows, int cols){
if(rows<=0||cols<=0){
return
}
int flag[55][55];
memset(flag,0,sizeof(flag));
dfs(threshold,rows,cols,0,0);
return ans.size();
}
};