class Solution {
public:
int help(int i){
int sum=0;
while(i!=0){
sum+=i%10;
i=i/10;
}
return sum;
}
int movingCount(int m, int n, int k) {
vector<vector<int>> dp(m,vector<int>(n,0));
dp[0][0] = 1;
int ans = 1;
vector<int> dir = {-1,0,1,0,-1};
for(int x=0;x<m;x++){
for(int y=0;y<n;y++){
for(int i=0;i<4;i++){
int dx=x+dir[i];
int dy=y+dir[i+1];
if(dx>=0&&dx<m&&dy>=0&&dy<n&&(help(x)+help(y))<=k&&dp[dx][dy]){
dp[x][y]=1;
ans+=1;
break;
}
}
}
}
return ans;
}
};
剑指 Offer 13. 机器人的运动范围
最新推荐文章于 2024-08-15 05:02:54 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)