AcWing 24. 机器人的运动范围
原题链接
简单
作者:
sy123
,
2021-01-10 23:32:09
,
所有人可见
,
阅读 345
class Solution {
public:
int getsum(pair<int,int> p){
int x=p.first;
int y=p.second;
int sum=0;
while(x){
sum+=x%10;
x/=10;
}
while(y){
sum+=y%10;
y/=10;
}
return sum;
}
int movingCount(int threshold, int rows, int cols)
{
if(!rows||!cols)return 0;
vector<vector<bool>>st(rows,vector<bool>(cols));
queue<pair<int,int>>q;
q.push({0,0});
st[0][0]=true;
int cnt=0;
int dx[4]={-1,0,1,0},dy[4]={0,1,0,-1};
while(q.size()){
auto t=q.front();
q.pop();
if(getsum(t)>threshold)continue;//不能用这个点更新周围的点
cnt++;
for(int i=0;i<4;i++){
int a=t.first+dx[i],b=t.second+dy[i];
if(a>=0&&a<rows&&b>=0&&b<cols&&!st[a][b]){
st[a][b]=true;
q.push({a,b});
}
}
}
return cnt;
}
};