------------------------------Got 小知识------------------------------------
char *name(){
char a[]="asdf";return a;
}
char a[]="asdf"; 这是一个数组,在栈分配空间,用"asdf"作初始化。函数退出后,这个栈分配的空间将被释放。
char*a = "asdf"; a是一个指针,指针字符串常量"asdf",这个常量是存在常量区的,整个程序生命期内都有效,
而返回的只是这个地址,而不是栈的地址,所以在主调函数中,是可以取到正常的内容
------------------------------------------------------------------Code------------------------------------------------
class Solution {
public:
int movingCount(int threshold, int rows, int cols)
{
if(threshold<0||rows<1||cols<1)return 0;
bool *visited=new bool[rows*cols];//这句 写成visited[]就不行了,,
memset(visited,0,rows*cols);
int count=C(threshold, rows, cols,0,0,visited);
return count;
}
int C(int k,int rows,int cols,int row,int col,bool*visited)
{
int count=0;
if(0<=row&&row<rows && 0<=col&&col<cols && go(row,col,k) && !visited[row*cols+col])//可以走
{
//count++;
visited[row*cols+col]=true;
count=1+C(k,rows, cols,row-1,col,visited)
+C(k,rows, cols,row+1,col,visited)
+C(k,rows, cols,row,col-1,visited)
+C(k,rows, cols,row,col+1,visited);
}
return count;
}
bool go(int x,int y,int k)
{
int sum=0;
int t;
while(x)
{
sum+=(x%10);
x=x/10;
}
if(sum>k)return false;
while(y)
{
sum+=(y%10);
y=y/10;
}
if(sum>k)return false;
return true;
}
};
Wuli 肖奈大神,Coding 源动力!!!