地上有一个 m行和 n 列的方格,横纵坐标范围分别是 0∼m−1 和 0∼n−1。
一个机器人从坐标 (0,0)(0,0) 的格子开始移动,每一次只能向左,右,上,下四个方向移动一格。
但是不能进入行坐标和列坐标的数位之和大于 k的格子。
请问该机器人能够达到多少个格子?
注意:
0<=m<=50
0<=n<=50
0<=k<=100
int dire[4][2]={{0,1},{-1,0},{0,-1},{1,0}}; int mat[50][50]; void dfs(int i, int j, int threshold, int rows, int cols, int *count) { //判断是否越界,是否走过(mat[i][j]==-1就是走过) if(i<0||j<0||i>=rows||j>=cols||mat[i][j]<0) { return ; } int t1=i%10+i/10; int t2=j%10+j/10; if(t1+t2>threshold) { mat[i][j]=1; //标记该位置不能走 return ; } else { (*count)++; mat[i][j]=-1; //标记该位置已经被走过了 for(int k=0;k<4;k++) { dfs(i+dire[k][0],j+dire[k][1],threshold,rows,cols,count); } } } int movingCount(int threshold, int rows, int cols) { if(rows<0||rows>50||cols<0||cols>50) return 0; int count=0; memset(mat,0,sizeof(mat)); dfs(0,0,threshold,rows,cols,&count); return count; }