c++ DFS
class Solution {
public:
int a[50][50];
int n=0;
bool in(int x,int y,int k)
{
if(x/10+x%10+y/10+y%10>k)
return false;
else
return true;
}
void dfs(int x,int y,int rows,int cols,int k)
{
if(x<0||x>rows-1||y<0||y>cols-1||!in(x,y,k)||a[x][y]==1)
return;
n++;
a[x][y]=1;
dfs(x-1,y,rows,cols,k);dfs(x,y-1,rows,cols,k);
dfs(x+1,y,rows,cols,k);dfs(x,y+1,rows,cols,k);
}
int movingCount(int threshold, int rows, int cols)
{
dfs(0,0,rows,cols,threshold);
return n;
}
};