-
Notifications
You must be signed in to change notification settings - Fork 28
/
面试题66之矩阵中的路径_StringPath.cpp
248 lines (198 loc) · 4.7 KB
/
面试题66之矩阵中的路径_StringPath.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
*
* Question Description:
* (Question 30 in <Coding Intervies>) How do you implement a function to check whether there is a
* path for a string in a matrix of characters? It moves to left, right, up, and down in a matrix,
* and a cell for a step. The path can start from any cell in a matrix. If a cell is occupied by a
* character of a string on the path, it cannot be occupied by another character again.
*/
#include <stdio.h>
#include <string>
#include <stack>
using namespace std;
bool hasPathCore(char* matrix, int rows, int cols, int row, int col, char* str, int& pathLength, bool* visited);
bool hasPath(char* matrix, int rows, int cols, char* str)
{
if(matrix == NULL || rows < 1 || cols < 1 || str == NULL)
return false;
bool *visited = new bool[rows * cols];
memset(visited, 0, rows * cols);
int pathLength = 0;
for(int row = 0; row < rows; ++row)
{
for(int col = 0; col < cols; ++col)
{
if(hasPathCore(matrix, rows, cols, row, col, str,
pathLength, visited))
{
return true;
}
}
}
delete[] visited;
return false;
}
bool hasPathCore(char* matrix, int rows, int cols, int row, int col, char* str, int& pathLength, bool* visited)
{
if(str[pathLength] == '\0')
return true;
bool hasPath = false;
if(row >= 0 && row < rows && col >= 0 && col < cols
&& matrix[row * cols + col] == str[pathLength]
&& !visited[row * cols + col])
{
++pathLength;
visited[row * cols + col] = true;
hasPath = hasPathCore(matrix, rows, cols, row, col - 1,
str, pathLength, visited)
|| hasPathCore(matrix, rows, cols, row - 1, col,
str, pathLength, visited)
|| hasPathCore(matrix, rows, cols, row, col + 1,
str, pathLength, visited)
|| hasPathCore(matrix, rows, cols, row + 1, col,
str, pathLength, visited);
if(!hasPath)
{
--pathLength;
visited[row * cols + col] = false;
}
}
return hasPath;
}
void Test(char* testName, char* matrix, int rows, int cols, char* str, bool expected)
{
if(testName != NULL)
printf("%s begins: ", testName);
if(hasPath(matrix, rows, cols, str) == expected)
printf("Passed.\n");
else
printf("FAILED.\n");
}
//ABCE
//SFCS
//ADEE
//ABCCED
void Test1()
{
char matrix[] = "ABCESFCSADEE";
char* str = "ABCCED";
Test("Test1", (char*)matrix, 3, 4, str, true);
}
//ABCE
//SFCS
//ADEE
//SEE
void Test2()
{
char matrix[] = "ABCESFCSADEE";
char* str = "SEE";
Test("Test2", (char*)matrix, 3, 4, str, true);
}
//ABCE
//SFCS
//ADEE
//ABCB
void Test3()
{
char matrix[] = "ABCESFCSADEE";
char* str = "ABCB";
Test("Test3", (char*)matrix, 3, 4, str, false);
}
//ABCEHJIG
//SFCSLOPQ
//ADEEMNOE
//ADIDEJFM
//VCEIFGGS
//SLHECCEIDEJFGGFIE
void Test4()
{
char matrix[] = "ABCEHJIGSFCSLOPQADEEMNOEADIDEJFMVCEIFGGS";
char* str = "SLHECCEIDEJFGGFIE";
Test("Test4", (char*)matrix, 5, 8, str, true);
}
//ABCEHJIG
//SFCSLOPQ
//ADEEMNOE
//ADIDEJFM
//VCEIFGGS
//SGGFIECVAASABCEHJIGQEM
void Test5()
{
char matrix[] = "ABCEHJIGSFCSLOPQADEEMNOEADIDEJFMVCEIFGGS";
char* str = "SGGFIECVAASABCEHJIGQEM";
Test("Test5", (char*)matrix, 5, 8, str, true);
}
//ABCEHJIG
//SFCSLOPQ
//ADEEMNOE
//ADIDEJFM
//VCEIFGGS
//SGGFIECVAASABCEEJIGOEM
void Test6()
{
char matrix[] = "ABCEHJIGSFCSLOPQADEEMNOEADIDEJFMVCEIFGGS";
char* str = "SGGFIECVAASABCEEJIGOEM";
Test("Test6", (char*)matrix, 5, 8, str, false);
}
//ABCEHJIG
//SFCSLOPQ
//ADEEMNOE
//ADIDEJFM
//VCEIFGGS
//SGGFIECVAASABCEHJIGQEMS
void Test7()
{
char matrix[] = "ABCEHJIGSFCSLOPQADEEMNOEADIDEJFMVCEIFGGS";
char* str = "SGGFIECVAASABCEHJIGQEMS";
Test("Test7", (char*)matrix, 5, 8, str, false);
}
//AAAA
//AAAA
//AAAA
//AAAAAAAAAAAA
void Test8()
{
char matrix[] = "AAAAAAAAAAAA";
char* str = "AAAAAAAAAAAA";
Test("Test8", (char*)matrix, 3, 4, str, true);
}
//AAAA
//AAAA
//AAAA
//AAAAAAAAAAAAA
void Test9()
{
char matrix[] = "AAAAAAAAAAAA";
char* str = "AAAAAAAAAAAAA";
Test("Test9", (char*)matrix, 3, 4, str, false);
}
//A
//A
void Test10()
{
char matrix[] = "A";
char* str = "A";
Test("Test10", (char*)matrix, 1, 1, str, true);
}
//A
//B
void Test11()
{
char matrix[] = "A";
char* str = "B";
Test("Test11", (char*)matrix, 1, 1, str, false);
}
int main(int argc, char* argv[])
{
Test1();
Test2();
Test3();
Test4();
Test5();
Test6();
Test7();
Test8();
Test9();
Test10();
Test11();
return 0;
}