Skip to content

Commit 0bc4f93

Browse files
committed
Chapter 08 section 06 codes updated.
1 parent f316e36 commit 0bc4f93

File tree

2 files changed

+64
-50
lines changed
  • 08-Recurion-and-Backstracking

2 files changed

+64
-50
lines changed
Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,42 @@
1-
/// Source : https://leetcode.com/problems/word-search/description/
2-
/// Author : liuyubobobo
3-
/// Time : 2017-05-05
4-
51
#include <iostream>
62
#include <vector>
73
#include <cassert>
84

95
using namespace std;
106

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)
1413
class Solution {
1514

1615
private:
1716
int d[4][2] = {{-1, 0}, {0,1}, {1, 0}, {0, -1}};
1817
int m, n;
1918
vector<vector<bool>> visited;
2019

21-
bool inArea( int x , int y ){
20+
bool inArea(int x, int y){
2221
return x >= 0 && x < m && y >= 0 && y < n;
2322
}
2423

2524
// 从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,
2726
int startx, int starty ){
2827

29-
//assert( inArea(startx,starty) );
30-
if( index == word.size() - 1 )
28+
//assert(inArea(startx,starty));
29+
if(index == word.size() - 1)
3130
return board[startx][starty] == word[index];
3231

33-
if( board[startx][starty] == word[index] ){
32+
if(board[startx][starty] == word[index]){
3433
visited[startx][starty] = true;
3534
// 从startx, starty出发,向四个方向寻
36-
for( int i = 0 ; i < 4 ; i ++ ){
35+
for(int i = 0 ; i < 4 ; i ++){
3736
int newx = startx + d[i][0];
3837
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))
4140
return true;
4241
}
4342
visited[startx][starty] = false;
@@ -49,12 +48,17 @@ class Solution {
4948
bool exist(vector<vector<char>>& board, string word) {
5049

5150
m = board.size();
52-
assert( m > 0 );
51+
assert(m > 0);
5352
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))
5862
return true;
5963

6064
return false;
@@ -63,30 +67,32 @@ class Solution {
6367

6468
int main() {
6569

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'}};
6773
vector<vector<char>> board1;
6874
for( int i = 0 ; i < 3 ; i ++ )
6975
board1.push_back(vector<char>(b1[i], b1[i] + sizeof(b1[i]) / sizeof(char)));
7076

7177
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 ++)
7480
if(Solution().exist(board1,words[i]))
75-
cout<<"found "<<words[i]<<endl;
81+
cout << "found " << words[i] << endl;
7682
else
77-
cout<<"can not found "<<words[i]<<endl;
83+
cout << "can not found " << words[i] << endl;
7884

7985
// ---
8086

8187
char b2[1][1] = {{'A'}};
8288
vector<vector<char>> board2;
8389
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)));
8591

8692
if(Solution().exist(board2,"AB"))
87-
cout<<"found AB"<<endl;
93+
cout << "found AB" << endl;
8894
else
89-
cout<<"can not found AB"<<endl;
95+
cout << "can not found AB" << endl;
9096

9197
return 0;
9298
}

08-Recurion-and-Backstracking/Course Code (Java)/06-Word-Search/src/Solution.java

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
// 回溯法
2-
// 时间复杂度: O(m*n*m*n)
3-
// 空间复杂度: O(m*n)
1+
/// 79. Word Search
2+
/// Source : https://leetcode.com/problems/word-search/description/
3+
///
4+
/// 回溯法
5+
/// 时间复杂度: O(m*n*m*n)
6+
/// 空间复杂度: O(m*n)
47
public class Solution {
58

6-
private int d[][] = {{-1,0},{0,1},{1,0},{0,-1}};
7-
private int m,n;
9+
private int d[][] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
10+
private int m, n;
811
private boolean[][] visited;
912

1013
public boolean exist(char[][] board, String word) {
@@ -13,13 +16,16 @@ public boolean exist(char[][] board, String word) {
1316
throw new IllegalArgumentException("board or word can not be null!");
1417

1518
m = board.length;
16-
assert( m > 0 );
19+
if(m == 0)
20+
throw new IllegalArgumentException("board can not be empty.");
1721
n = board[0].length;
22+
if(n == 0)
23+
throw new IllegalArgumentException("board can not be empty.");
1824

1925
visited = new boolean[m][n];
20-
for( int i = 0 ; i < m ; i ++ )
21-
for( int j = 0 ; j < n ; j ++ )
22-
if( searchWord( board, word, 0 , i, j) )
26+
for(int i = 0 ; i < m ; i ++)
27+
for(int j = 0 ; j < n ; j ++)
28+
if(searchWord(board, word, 0, i, j))
2329
return true;
2430

2531
return false;
@@ -30,21 +36,21 @@ private boolean inArea( int x , int y ){
3036
}
3137

3238
// 从board[startx][starty]开始, 寻找word[index...word.size())
33-
private boolean searchWord( char[][] board, String word, int index,
34-
int startx, int starty ){
39+
private boolean searchWord(char[][] board, String word, int index,
40+
int startx, int starty){
3541

36-
//assert( inArea(startx,starty) );
37-
if( index == word.length() - 1 )
42+
//assert(inArea(startx,starty));
43+
if(index == word.length() - 1)
3844
return board[startx][starty] == word.charAt(index);
3945

40-
if( board[startx][starty] == word.charAt(index) ){
46+
if(board[startx][starty] == word.charAt(index)){
4147
visited[startx][starty] = true;
4248
// 从startx, starty出发,向四个方向寻
43-
for( int i = 0 ; i < 4 ; i ++ ){
49+
for(int i = 0 ; i < 4 ; i ++){
4450
int newx = startx + d[i][0];
4551
int newy = starty + d[i][1];
46-
if( inArea(newx, newy) && !visited[newx][newy] &&
47-
searchWord( board , word , index + 1 , newx , newy ) )
52+
if(inArea(newx, newy) && !visited[newx][newy] &&
53+
searchWord(board, word, index + 1, newx, newy))
4854
return true;
4955
}
5056
visited[startx][starty] = false;
@@ -54,19 +60,21 @@ private boolean searchWord( char[][] board, String word, int index,
5460

5561
public static void main(String args[]){
5662

57-
char[][] b1 = {{'A','B','C','E'},{'S','F','C','S'},{'A','D','E','E'}};
63+
char[][] b1 = { {'A','B','C','E'},
64+
{'S','F','C','S'},
65+
{'A','D','E','E'}};
5866

59-
String words[] = {"ABCCED" , "SEE" , "ABCB" };
60-
for( int i = 0 ; i < words.length ; i ++ )
61-
if( (new Solution()).exist(b1, words[i]))
67+
String words[] = {"ABCCED", "SEE", "ABCB" };
68+
for(int i = 0 ; i < words.length ; i ++)
69+
if((new Solution()).exist(b1, words[i]))
6270
System.out.println("found " + words[i]);
6371
else
6472
System.out.println("can not found " + words[i]);
6573

6674
// ---
6775

6876
char[][] b2 = {{'A'}};
69-
if( (new Solution()).exist(b2,"AB"))
77+
if((new Solution()).exist(b2,"AB"))
7078
System.out.println("found AB");
7179
else
7280
System.out.println("can not found AB");

0 commit comments

Comments
 (0)