-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcoding_interview_13_RobotMove.cpp
213 lines (187 loc) · 4.79 KB
/
coding_interview_13_RobotMove.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*******************************************************************
Copyright(c) 2016, Harry He
All rights reserved.
Distributed under the BSD license.
(See accompanying file LICENSE.txt at
https://github.com/zhedahht/CodingInterviewChinese2/blob/master/LICENSE.txt)
*******************************************************************/
//==================================================================
// 《剑指Offer——名企面试官精讲典型编程题》代码
// 作者:何海涛
//==================================================================
// 面试题13:机器人的运动范围
// 题目:地上有一个m行n列的方格。一个机器人从坐标(0, 0)的格子开始移动,它
// 每一次可以向左、右、上、下移动一格,但不能进入行坐标和列坐标的数位之和
// 大于k的格子。例如,当k为18时,机器人能够进入方格(35, 37),因为3+5+3+7=18。
// 但它不能进入方格(35, 38),因为3+5+3+8=19。请问该机器人能够到达多少个格子?
#include <cstdio>
#include<vector>
using namespace std;
int movingCountCore(int threshold, int rows, int cols, int row, int col, bool* visited);
bool check(int threshold, int rows, int cols, int row, int col, bool* visited);
int getDigitSum(int number);
// My Solution
bool check(int threshold, const int& rows, const int& cols, int r, int c) {
bool flag = (0 <= r && r < rows && 0 <= c && c < cols);
while (r > 0 && threshold>=0) {
threshold -= (r % 10);
r /= 10;
}
while (c > 0 && threshold>=0) {
threshold -= (c % 10);
c /= 10;
}
return flag && threshold >= 0;
}
const int dr[] = { -1,1,0,0 };
const int dc[] = { 0,0,-1,1 };
void dfs(int threshold, int rows, int cols, int r, int c, vector<vector<int>>& g) {
if (check(threshold, rows, cols, r, c)) {
g[r][c] = 1;
for (int i = 0; i < 4; ++i) {
int rr = r + dr[i], cc = c + dc[i];
if (0 <= rr && rr < rows && 0 <= cc && cc < cols && g[rr][cc] == 0) {
dfs(threshold, rows, cols, rr, cc, g);
}
}
}
}
int movingCount(int threshold, int rows, int cols) {
vector<vector<int>> g(rows, vector<int>(cols, 0));
dfs(threshold, rows, cols, 0, 0, g);
int cnt = 0;
for (int r = 0; r < rows; ++r)
for (int c = 0; c < cols; ++c) {
if (g[r][c] == 1)
++cnt;
}
return cnt;
}
//int movingCount(int threshold, int rows, int cols)
//{
// if (threshold < 0 || rows <= 0 || cols <= 0)
// return 0;
//
// bool* visited = new bool[rows * cols];
// for (int i = 0; i < rows * cols; ++i)
// visited[i] = false;
//
// int count = movingCountCore(threshold, rows, cols,
// 0, 0, visited);
//
// delete[] visited;
//
// return count;
//}
//
//int movingCountCore(int threshold, int rows, int cols, int row,
// int col, bool* visited)
//{
// int count = 0;
// if (check(threshold, rows, cols, row, col, visited))
// {
// visited[row * cols + col] = true;
//
// count = 1 + movingCountCore(threshold, rows, cols,
// row - 1, col, visited)
// + movingCountCore(threshold, rows, cols,
// row, col - 1, visited)
// + movingCountCore(threshold, rows, cols,
// row + 1, col, visited)
// + movingCountCore(threshold, rows, cols,
// row, col + 1, visited);
// }
//
// return count;
//}
//
//bool check(int threshold, int rows, int cols, int row, int col,
// bool* visited)
//{
// if (row >= 0 && row < rows && col >= 0 && col < cols
// && getDigitSum(row) + getDigitSum(col) <= threshold
// && !visited[row * cols + col])
// return true;
//
// return false;
//}
//
//int getDigitSum(int number)
//{
// int sum = 0;
// while (number > 0)
// {
// sum += number % 10;
// number /= 10;
// }
//
// return sum;
//}
// ====================测试代码====================
void test(const char* testName, int threshold, int rows, int cols, int expected)
{
if (testName != nullptr)
printf("%s begins: ", testName);
if (movingCount(threshold, rows, cols) == expected)
printf("Passed.\n");
else
printf("FAILED.\n");
}
// 方格多行多列
void test1()
{
test("Test1", 5, 10, 10, 21);
}
// 方格多行多列
void test2()
{
test("Test2", 15, 20, 20, 359);
}
// 方格只有一行,机器人只能到达部分方格
void test3()
{
test("Test3", 10, 1, 100, 29);
}
// 方格只有一行,机器人能到达所有方格
void test4()
{
test("Test4", 10, 1, 10, 10);
}
// 方格只有一列,机器人只能到达部分方格
void test5()
{
test("Test5", 15, 100, 1, 79);
}
// 方格只有一列,机器人能到达所有方格
void test6()
{
test("Test6", 15, 10, 1, 10);
}
// 方格只有一行一列
void test7()
{
test("Test7", 15, 1, 1, 1);
}
// 方格只有一行一列
void test8()
{
test("Test8", 0, 1, 1, 1);
}
// 机器人不能进入任意一个方格
void test9()
{
test("Test9", -10, 10, 10, 0);
}
int main(int agrc, char* argv[])
{
test1();
test2();
test3();
test4();
test5();
test6();
test7();
test8();
test9();
return 0;
}