Skip to content

Commit d9ce659

Browse files
committedFeb 13, 2022
feat: update solutions to lcof problem: No.13
面试题13.机器人的运动范围
1 parent 96911f0 commit d9ce659

File tree

21 files changed

+1001
-333
lines changed

21 files changed

+1001
-333
lines changed
 

‎lcof/面试题12. 矩阵中的路径/README.md

+30-19
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,45 @@
22

33
## 题目描述
44

5-
给定一个 `m x n` 二维字符网格 `board` 和一个字符串单词 `word`。如果 `word` 存在于网格中,返回 `true`;否则,返回 `false`
5+
<p>给定一个 <code>m x n</code> 二维字符网格 <code>board</code> 和一个字符串单词 <code>word</code> 。如果 <code>word</code> 存在于网格中,返回 <code>true</code> ;否则,返回 <code>false</code> 。</p>
66

7-
单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母不允许被重复使用。
7+
<p>单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母不允许被重复使用。</p>
88

9+
<p> </p>
910

10-
例如,在下面的 3×4 的矩阵中包含单词 "ABCCED"(单词中的字母已标出)。
11+
<p>例如,在下面的 3×4 的矩阵中包含单词 "ABCCED"(单词中的字母已标出)。</p>
1112

12-
![](./images/word2.jpg)
13+
<p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/lcof/%E9%9D%A2%E8%AF%95%E9%A2%9812.%20%E7%9F%A9%E9%98%B5%E4%B8%AD%E7%9A%84%E8%B7%AF%E5%BE%84/images/word2.jpg" style="width: 322px; height: 242px;" /></p>
1314

14-
**示例 1:**
15+
<p> </p>
1516

16-
```
17-
输入:board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCCED"
18-
输出:true
19-
```
17+
<p><strong>示例 1:</strong></p>
2018

21-
**示例 2:**
19+
<pre>
20+
<strong>输入:</strong>board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCCED"
21+
<strong>输出:</strong>true
22+
</pre>
2223

23-
```
24-
输入:board = [["a","b"],["c","d"]], word = "abcd"
25-
输出:false
26-
```
24+
<p><strong>示例 2:</strong></p>
25+
26+
<pre>
27+
<strong>输入:</strong>board = [["a","b"],["c","d"]], word = "abcd"
28+
<strong>输出:</strong>false
29+
</pre>
30+
31+
<p> </p>
32+
33+
<p><strong>提示:</strong></p>
34+
35+
<ul>
36+
<li><code>1 <= board.length <= 200</code></li>
37+
<li><code>1 <= board[i].length <= 200</code></li>
38+
<li><code>board</code> 和 <code>word</code> 仅由大小写英文字母组成</li>
39+
</ul>
2740

28-
**提示:**
41+
<p> </p>
2942

30-
- `1 <= board.length <= 200`
31-
- `1 <= board[i].length <= 200`
32-
- `board``word` 仅由大小写英文字母组成
43+
<p><strong>注意:</strong>本题与主站 79 题相同:<a href="https://leetcode-cn.com/problems/word-search/">https://leetcode-cn.com/problems/word-search/</a></p>
3344

3445
## 解法
3546

@@ -180,7 +191,7 @@ public:
180191
for (int j = 0; j < board[0].size(); ++j)
181192
if (dfs(i, j, 0, board, word))
182193
return 1;
183-
return 0;
194+
return 0;
184195
}
185196

186197
bool dfs(int i, int j, int k, vector<vector<char>>& board, string word) {

‎lcof/面试题13. 机器人的运动范围/README.md

+73-148
Original file line numberDiff line numberDiff line change
@@ -2,95 +2,73 @@
22

33
## 题目描述
44

5-
地上有一个 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。请问该机器人能够到达多少个格子?
5+
<p>地上有一个m行n列的方格,从坐标 <code>[0,0]</code> 到坐标 <code>[m-1,n-1]</code> 。一个机器人从坐标 <code>[0, 0] </code>的格子开始移动,它每次可以向左、右、上、下移动一格(不能移动到方格外),也不能进入行坐标和列坐标的数位之和大于k的格子。例如,当k为18时,机器人能够进入方格 [35, 37] ,因为3+5+3+7=18。但它不能进入方格 [35, 38],因为3+5+3+8=19。请问该机器人能够到达多少个格子?</p>
66

7-
**示例 1:**
7+
<p>&nbsp;</p>
88

9-
```
10-
输入:m = 2, n = 3, k = 1
11-
输出:3
12-
```
9+
<p><strong>示例 1:</strong></p>
1310

14-
**示例 2:**
11+
<pre><strong>输入:</strong>m = 2, n = 3, k = 1
12+
<strong>输出:</strong>3
13+
</pre>
1514

16-
```
17-
输入:m = 3, n = 1, k = 0
18-
输出:1
19-
```
15+
<p><strong>示例 2:</strong></p>
2016

21-
**提示:**
17+
<pre><strong>输入:</strong>m = 3, n = 1, k = 0
18+
<strong>输出:</strong>1
19+
</pre>
2220

23-
- `1 <= n,m <= 100`
24-
- `0 <= k <= 20`
21+
<p><strong>提示:</strong></p>
22+
23+
<ul>
24+
<li><code>1 &lt;= n,m &lt;= 100</code></li>
25+
<li><code>0 &lt;= k&nbsp;&lt;= 20</code></li>
26+
</ul>
2527

2628
## 解法
2729

28-
深度优先搜索 DFS 实现
30+
从坐标 `(0, 0)` 开始,往右、下两个方向开始深搜
2931

3032
<!-- tabs:start -->
3133

3234
### **Python3**
3335

3436
```python
3537
class Solution:
36-
cnt = 0
3738
def movingCount(self, m: int, n: int, k: int) -> int:
38-
def cal(m, n):
39-
s = str(m) + str(n)
40-
return sum([int(i) for i in s])
4139
def dfs(i, j):
42-
if i < 0 or i >= m or j < 0 or j >= n or cal(i, j) > k or visited[i][j]:
43-
return
44-
self.cnt += 1
45-
visited[i][j] = True
46-
dfs(i + 1, j)
47-
dfs(i - 1, j)
48-
dfs(i, j + 1)
49-
dfs(i, j - 1)
50-
self.cnt = 0
51-
visited = [[False for _ in range(n)] for _ in range(m)]
52-
dfs(0, 0)
53-
return self.cnt
40+
if i >= m or j >= n or vis[i][j] or (i % 10 + i // 10 + j % 10 + j // 10) > k:
41+
return 0
42+
vis[i][j] = True
43+
return 1 + dfs(i + 1, j) + dfs(i, j + 1)
44+
45+
vis = [[False] * n for _ in range(m)]
46+
return dfs(0, 0)
5447
```
5548

5649
### **Java**
5750

5851
```java
5952
class Solution {
53+
private boolean[][] vis;
6054
private int m;
6155
private int n;
62-
private boolean[][] visited;
63-
private int cnt;
56+
private int k;
57+
6458
public int movingCount(int m, int n, int k) {
65-
visited = new boolean[m][n];
6659
this.m = m;
6760
this.n = n;
68-
cnt = 0;
69-
dfs(0, 0, k);
70-
return cnt;
61+
this.k = k;
62+
vis = new boolean[m][n];
63+
return dfs(0, 0);
7164
}
7265

73-
private void dfs(int i, int j, int k) {
74-
if (i < 0 || i >= m || j < 0 || j >= n || visited[i][j] || cal(i, j) > k) return;
75-
++cnt;
76-
visited[i][j] = true;
77-
dfs(i + 1, j, k);
78-
dfs(i - 1, j, k);
79-
dfs(i, j + 1, k);
80-
dfs(i, j - 1, k);
81-
}
82-
83-
private int cal(int i, int j) {
84-
int res = 0;
85-
while (i != 0) {
86-
res += (i % 10);
87-
i /= 10;
88-
}
89-
while (j != 0) {
90-
res += (j % 10);
91-
j /= 10;
66+
private int dfs(int i, int j) {
67+
if (i >= m || j >= n || vis[i][j] || (i % 10 + i / 10 + j % 10 + j / 10) > k) {
68+
return 0;
9269
}
93-
return res;
70+
vis[i][j] = true;
71+
return 1 + dfs(i + 1, j) + dfs(i, j + 1);
9472
}
9573
}
9674
```
@@ -105,71 +83,40 @@ class Solution {
10583
* @return {number}
10684
*/
10785
var movingCount = function (m, n, k) {
108-
let res = 0;
109-
let isRead = [...new Array(m)].map(() => Array(n).fill(0));
110-
let moving = [
111-
[0, -1],
112-
[0, 1],
113-
[1, 0],
114-
[-1, 0],
115-
];
116-
let queue = [[0, 0]];
117-
isRead[0][0] = 1;
118-
while (queue.length) {
119-
let [x, y] = queue.shift();
120-
for (let [dx, dy] of moving) {
121-
let X = x + dx;
122-
let Y = y + dy;
123-
if (
124-
X >= 0 &&
125-
Y >= 0 &&
126-
X < m &&
127-
Y < n &&
128-
!isRead[X][Y] &&
129-
isValid(X, Y)
130-
) {
131-
queue.push([X, Y]);
132-
isRead[X][Y] = 1;
133-
}
86+
const vis = new Array(m * n).fill(false);
87+
let dfs = function (i, j) {
88+
if (
89+
i >= m ||
90+
j >= n ||
91+
vis[i * n + j] ||
92+
(i % 10) + Math.floor(i / 10) + (j % 10) + Math.floor(j / 10) > k
93+
) {
94+
return 0;
13495
}
135-
res++;
136-
}
137-
function isValid(x, y) {
138-
let r = 0;
139-
r +=
140-
x
141-
.toString()
142-
.split("")
143-
.reduce((acc, cur) => acc + +cur, 0) +
144-
y
145-
.toString()
146-
.split("")
147-
.reduce((acc, cur) => acc + +cur, 0);
148-
if (r <= k) return true;
149-
else return false;
150-
}
151-
return res;
96+
vis[i * n + j] = true;
97+
return 1 + dfs(i + 1, j) + dfs(i, j + 1);
98+
};
99+
return dfs(0, 0);
152100
};
153101
```
154102

155103
### **Go**
156104

157105
```go
158106
func movingCount(m int, n int, k int) int {
159-
var visited [][]bool
160-
visited = make([][]bool, m)
161-
for i := 0; i < m; i++ {
162-
visited[i] = make([]bool, n)
107+
vis := make([][]bool, m)
108+
for i := range vis {
109+
vis[i] = make([]bool, n)
163110
}
164-
return dfs(0, 0, m, n, k, visited)
165-
}
166-
167-
func dfs(x, y, m, n, k int, visited [][]bool) int {
168-
if x >= m || y >= n || visited[x][y] || (x%10+x/10+y%10+y/10) > k {
169-
return 0
111+
var dfs func(i, j int) int
112+
dfs = func(i, j int) int {
113+
if i >= m || j >= n || vis[i][j] || (i%10+i/10+j%10+j/10) > k {
114+
return 0
115+
}
116+
vis[i][j] = true
117+
return 1 + dfs(i+1, j) + dfs(i, j+1)
170118
}
171-
visited[x][y] = true
172-
return 1 + dfs(x+1, y, m, n, k, visited) + dfs(x, y+1, m, n, k, visited)
119+
return dfs(0, 0)
173120
}
174121
```
175122

@@ -178,45 +125,23 @@ func dfs(x, y, m, n, k int, visited [][]bool) int {
178125
```cpp
179126
class Solution {
180127
public:
181-
int checksum(int m, int n, int target) {
182-
int a = 0;
183-
while (m > 0) {
184-
a += m % 10;
185-
m /= 10;
186-
}
187-
188-
int b = 0;
189-
while (n > 0) {
190-
b += n % 10;
191-
n /= 10;
192-
}
193-
194-
return a + b <= target;
195-
}
196-
197-
int moving(int row, int col, vector<vector<int>>& arr, int i, int j, int target) {
198-
int count = 0;
199-
if (checksum(i, j, target)
200-
&& i>=0 && i < row && j>=0 && j < col
201-
&& arr[i][j] == 0) {
202-
arr[i][j] = 1;
203-
count = 1 + moving(row, col, arr, i-1, j, target)
204-
+ moving(row, col, arr, i, j-1, target)
205-
+ moving(row, col, arr, i+1, j, target)
206-
+ moving(row, col, arr, i, j+1, target);
207-
}
208-
209-
return count;
210-
}
128+
int m;
129+
int n;
130+
int k;
131+
vector<vector<bool>> vis;
211132

212133
int movingCount(int m, int n, int k) {
213-
if (m == 0 || n == 0) {
214-
return 0;
215-
}
134+
this->m = m;
135+
this->n = n;
136+
this->k = k;
137+
vis.resize(m, vector<bool>(n, false));
138+
return dfs(0, 0);
139+
}
216140

217-
vector<vector<int>> arr(m, vector<int>(n, 0));
218-
int cnt = moving(m, n, arr, 0, 0, k);
219-
return cnt;
141+
int dfs(int i, int j) {
142+
if (i >= m || j >= n || vis[i][j] || (i % 10 + i / 10 + j % 10 + j / 10) > k) return 0;
143+
vis[i][j] = true;
144+
return 1 + dfs(i + 1, j) + dfs(i, j + 1);
220145
}
221146
};
222147
```

0 commit comments

Comments
 (0)
Please sign in to comment.