首页 技术 正文
技术 2022年11月10日
0 收藏 401 点赞 3,245 浏览 1737 个字

目录

题目地址:https://leetcode-cn.com/problems/ji-qi-ren-de-yun-dong-fan-wei-lcof/

题目描述

地上有一个m行n列的方格,从坐标 [0,0] 到坐标 [m-1,n-1] 。一个机器人从坐标 [0, 0] 的格子开始移动,它每次可以向左、右、上、下移动一格(不能移动到方格外),也不能进入行坐标和列坐标的数位之和大于k的格子。

例如,当k为18时,机器人能够进入方格 [35, 37] ,因为3+5+3+7=18。但它不能进入方格 [35, 38],因为3+5+3+8=19。请问该机器人能够到达多少个格子?

示例 1:

输入:m = 2, n = 3, k = 1
输出:3

示例 1:

输入:m = 3, n = 1, k = 0
输出:1

提示:

  1. 1 <= n,m <= 100
  2. 0 <= k <= 20

题目大意

给出了限制,看机器人能到哪些位置。

解题方法

BFS

本题要求总的有多少个点符合要求,本质是个搜索问题。搜索问题可以用 BFS 或者 DFS,都有相应的模板。

我在博客中已经总结了所有常见的算法模板,【LeetCode】代码模板,刷题必会,直接拿来用!

BFS使用队列,把每个还没有搜索到的点一次放入队列,然后再弹出队列的头部元素当做当前遍历点。

如果不需要确定当前遍历到了哪一层,BFS模板如下。

while queue 不空:
cur = queue.pop()
for 节点 in cur的所有相邻节点:
if 该节点有效且未访问过:
queue.push(该节点)

如果要确定当前遍历到了哪一层,BFS模板如下。这里增加了level表示当前遍历到二叉树中的哪一层了,也可以理解为在一个图中,现在已经走了多少步了。size表示在开始遍历新的一层时,队列中有多少个元素,即有多少个点需要向前走一步。

level = 0
while queue 不空:
size = queue.size()
while (size --) {
cur = queue.pop()
for 节点 in cur的所有相邻节点:
if 该节点有效且未被访问过:
queue.push(该节点)
}
level ++;

上面两个是通用模板,在任何题目中都可以用,是要记住的!

本题不需要记录遍历到多少层,只需要统计总的遍历了多少个点,因此使用模板一。

保存一个点是否遍历过:使用了visited数组,如果一个点放入队列,那么同时就设置其已经visited了。

直接使用模板,C++代码如下:

class Solution {
public:
int movingCount(int m, int n, int k) {
vector<vector<int>> visited(m, vector<int>(n, 0));
queue<pair<int, int>> q;
q.push({0, 0});
int res = 0;
visited[0][0] = 1;
while (!q.empty()) {
auto front = q.front(); q.pop();
int x = front.first;
int y = front.second;
res += 1;
for (auto d : directions) {
int new_x = x + d.first;
int new_y = y + d.second;
if (new_x < 0 || new_x >= m || new_y < 0 || new_y >= n
|| visited[new_x][new_y] == 1 ||
sumDigit(new_x, new_y) > k) {
continue;
}
q.push({new_x, new_y});
visited[new_x][new_y] = 1;
}
}
return res;
}
int sumDigit(int i, int j) {
int sum = 0;
while (i > 0) {
sum += i % 10;
i /= 10;
}
while (j > 0) {
sum += j % 10;
j /= 10;
}
return sum;
}
private:
vector<pair<int, int>> directions = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
};

欢迎关注负雪明烛的刷题博客,leetcode刷题800多,每道都讲解了详细写法!

日期

2020 年 4 月 8 日 —— 天气暖和了

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,489
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,904
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,737
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,490
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,128
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,291