当前位置:网站首页>剑指 Offer 13. 机器人的运动范围
剑指 Offer 13. 机器人的运动范围
2022-06-29 23:06:00 【anieoo】
solution:
bfs模板题
class Solution {
public:
typedef pair<int,int> PII;
int dx[4] = {-1, 0, 1, 0},dy[4] = {0, -1, 0, 1};
vector<vector<bool>> sta;
int movingCount(int m, int n, int k) {
if(k == 0) return 1; //k = 0只能到达(0,0)
int ans = 0; //返回值
sta = vector<vector<bool>> (m, vector<bool> (n, false));
queue<PII> que;
que.push({0,0}); //起点入栈
sta[0][0] = true;
while(!que.empty()) {
auto t = que.front();
que.pop();
ans++;
for(int i = 0;i < 4;i++) {
int x = t.first + dx[i];
int y = t.second + dy[i];
if(x < 0 || x == m || y < 0 || y == n || sta[x][y] || sum(x) + sum(y) > k) continue;
sta[x][y] = true;
que.push({x,y});
}
}
return ans;
}
//sum函数计算各位数字之和
int sum(int x) {
int ans = 0;
while(x) {
ans += x % 10;
x /= 10;
}
return ans;
}
};
边栏推荐
- 为什么 JSX 语法这么香?
- 80-Redis详解
- 自己收藏的一些网址
- C language tutorial – -6 loop statement
- Status acquisition and control system of on-site express cabinet
- 二叉搜索树 230. 二叉搜索树中第K小的元素 1038. 从二叉搜索树到更大和树
- 记一次排查线上MySQL死锁过程,不能只会curd,还要知道加锁原理
- Unity Pac Man games, maze implementation
- Incluxdb time series database system
- Gracefully transform the SMS business module and start the strategic mode!
猜你喜欢
How to solve the problem that the computer time is not automatically updated after proofreading
matplotlib matplotlib中plt.hist()参数解释
VS无法定位程序输入点于动态链接库
2022 PMP project management examination agile knowledge points (5)
架构实战营模块 5 作业
Leetcode 1385. Distance value between two arrays
The server quickly sets up the alist integrated network disk website [pagoda panel one click deployment of alist]
新钛云服荣膺“2022爱分析 · IT运维厂商全景报告”云管理平台CMP 代表厂商!...
开源了 | 文心大模型ERNIE-Tiny轻量化技术,又准又快,效果全开
redis客户端
随机推荐
深度学习的历史
自己收藏的一些网址
二叉搜索树 230. 二叉搜索树中第K小的元素 1038. 从二叉搜索树到更大和树
uniapp复制内容到剪贴板
CE second operation
How can the local / park do a good job in industrial analysis?
动态代理的实现原理
Still stay up late every day and work overtime to make statements? In fact, you don't know how to make reports efficiently
二叉树的序列化 力扣 297. 二叉树的序列化与反序列化 652. 寻找重复的子树
Cacti关于spine轮询的设置
架构实战营模块 5 作业
Effective self summary of remote communication | community essay solicitation
关于二叉树
大学里遗憾的事,希望你无怨也无悔
Collection! Have you ever used these tools to improve programmer productivity?
Evaluation of powerful and excellent document management software: image management, book management and document management
开源了 | 文心大模型ERNIE-Tiny轻量化技术,又准又快,效果全开
Uniapp copy contents to clipboard
MetaQ集群安裝測試
Metaq cluster installation test