1
- // / Source : https://leetcode.com/problems/word-search/description/
2
- // / Author : liuyubobobo
3
- // / Time : 2017-05-05
4
-
5
1
#include < iostream>
6
2
#include < vector>
7
3
#include < cassert>
8
4
9
5
using namespace std ;
10
6
11
- // 回溯法
12
- // 时间复杂度: O(m*n*m*n)
13
- // 空间复杂度: O(m*n)
7
+ // / 79. Word Search
8
+ // / Source : https://leetcode.com/problems/word-search/description/
9
+ // /
10
+ // / 回溯法
11
+ // / 时间复杂度: O(m*n*m*n)
12
+ // / 空间复杂度: O(m*n)
14
13
class Solution {
15
14
16
15
private:
17
16
int d[4 ][2 ] = {{-1 , 0 }, {0 ,1 }, {1 , 0 }, {0 , -1 }};
18
17
int m, n;
19
18
vector<vector<bool >> visited;
20
19
21
- bool inArea ( int x , int y ){
20
+ bool inArea (int x, int y){
22
21
return x >= 0 && x < m && y >= 0 && y < n;
23
22
}
24
23
25
24
// 从board[startx][starty]开始, 寻找word[index...word.size())
26
- bool searchWord ( const vector<vector<char >> &board, const string& word, int index,
25
+ bool searchWord (const vector<vector<char >> &board, const string& word, int index,
27
26
int startx, int starty ){
28
27
29
- // assert( inArea(startx,starty) );
30
- if ( index == word.size () - 1 )
28
+ // assert(inArea(startx,starty));
29
+ if (index == word.size () - 1 )
31
30
return board[startx][starty] == word[index];
32
31
33
- if ( board[startx][starty] == word[index] ){
32
+ if (board[startx][starty] == word[index]){
34
33
visited[startx][starty] = true ;
35
34
// 从startx, starty出发,向四个方向寻
36
- for ( int i = 0 ; i < 4 ; i ++ ){
35
+ for (int i = 0 ; i < 4 ; i ++){
37
36
int newx = startx + d[i][0 ];
38
37
int newy = starty + d[i][1 ];
39
- if ( inArea (newx, newy) && !visited[newx][newy] &&
40
- searchWord (board, word, index + 1 , newx, newy))
38
+ if (inArea (newx, newy) && !visited[newx][newy] &&
39
+ searchWord (board, word, index + 1 , newx, newy))
41
40
return true ;
42
41
}
43
42
visited[startx][starty] = false ;
@@ -49,12 +48,17 @@ class Solution {
49
48
bool exist (vector<vector<char >>& board, string word) {
50
49
51
50
m = board.size ();
52
- assert ( m > 0 );
51
+ assert (m > 0 );
53
52
n = board[0 ].size ();
54
- visited = vector<vector<bool >>(m, vector<bool >(n, false ));
55
- for ( int i = 0 ; i < board.size () ; i ++ )
56
- for ( int j = 0 ; j < board[i].size () ; j ++ )
57
- if ( searchWord ( board, word, 0 , i, j) )
53
+ assert (n > 0 );
54
+
55
+ visited.clear ();
56
+ for (int i = 0 ; i < m ; i ++)
57
+ visited.push_back (vector<bool >(n, false ));
58
+
59
+ for (int i = 0 ; i < board.size () ; i ++)
60
+ for (int j = 0 ; j < board[i].size () ; j ++)
61
+ if (searchWord (board, word, 0 , i, j))
58
62
return true ;
59
63
60
64
return false ;
@@ -63,30 +67,32 @@ class Solution {
63
67
64
68
int main () {
65
69
66
- char b1[3 ][4 ] = {{' A' ,' B' ,' C' ,' E' },{' S' ,' F' ,' C' ,' S' },{' A' ,' D' ,' E' ,' E' }};
70
+ char b1[3 ][4 ] = {{' A' , ' B' , ' C' , ' E' },
71
+ {' S' , ' F' , ' C' , ' S' },
72
+ {' A' , ' D' , ' E' , ' E' }};
67
73
vector<vector<char >> board1;
68
74
for ( int i = 0 ; i < 3 ; i ++ )
69
75
board1.push_back (vector<char >(b1[i], b1[i] + sizeof (b1[i]) / sizeof (char )));
70
76
71
77
int cases = 3 ;
72
- string words[3 ] = {" ABCCED" , " SEE" , " ABCB" };
73
- for ( int i = 0 ; i < cases ; i ++ )
78
+ string words[3 ] = {" ABCCED" , " SEE" , " ABCB" };
79
+ for (int i = 0 ; i < cases ; i ++)
74
80
if (Solution ().exist (board1,words[i]))
75
- cout<< " found " << words[i]<< endl;
81
+ cout << " found " << words[i] << endl;
76
82
else
77
- cout<< " can not found " << words[i]<< endl;
83
+ cout << " can not found " << words[i] << endl;
78
84
79
85
// ---
80
86
81
87
char b2[1 ][1 ] = {{' A' }};
82
88
vector<vector<char >> board2;
83
89
for (int i = 0 ; i < 3 ; i ++)
84
- board2.push_back (vector<char >(b2[i],b2[i]+ sizeof (b2[i])/sizeof (char )));
90
+ board2.push_back (vector<char >(b2[i], b2[i] + sizeof (b2[i])/sizeof (char )));
85
91
86
92
if (Solution ().exist (board2," AB" ))
87
- cout<< " found AB" << endl;
93
+ cout << " found AB" << endl;
88
94
else
89
- cout<< " can not found AB" << endl;
95
+ cout << " can not found AB" << endl;
90
96
91
97
return 0 ;
92
98
}
0 commit comments