forked from haoel/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wordSearch.cpp
132 lines (111 loc) · 3.77 KB
/
wordSearch.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
// Source : https://oj.leetcode.com/problems/word-search/
// Author : Hao Chen
// Date : 2014-07-19
/**********************************************************************************
*
* Given a 2D board and a word, find if the word exists in the grid.
*
* The word can be constructed from letters of sequentially adjacent cell,
* where "adjacent" cells are those horizontally or vertically neighboring.
* The same letter cell may not be used more than once.
*
* For example,
* Given board =
*
* [
* ["ABCE"],
* ["SFCS"],
* ["ADEE"]
* ]
*
* word = "ABCCED", -> returns true,
* word = "SEE", -> returns true,
* word = "ABCB", -> returns false.
*
*
**********************************************************************************/
#include <iostream>
#include <vector>
#include <string>
using namespace std;
//Recursive backtracking algorithm
bool exist(vector<vector<char> > &board, string word, int idx, int row, int col, vector< vector<int> > &mask) {
int i = row;
int j = col;
if (board[i][j] == word[idx] && mask[i][j]==0 ) {
mask[i][j]=1; //mark the current char is matched
if (idx+1 == word.size()) return true;
//checking the next char in `word` through the right, left, up, down four directions in the `board`.
idx++;
if (( i+1<board.size() && exist(board, word, idx, i+1, j, mask) ) ||
( i>0 && exist(board, word, idx, i-1, j, mask) ) ||
( j+1<board[i].size() && exist(board, word, idx, i, j+1, mask) ) ||
( j>0 && exist(board, word, idx, i, j-1, mask) ) )
{
return true;
}
mask[i][j]=0; //cannot find any successful solution, clear the mark. (backtracking)
}
return false;
}
bool exist(vector<vector<char> > &board, string word) {
if (board.size()<=0 || word.size()<=0) return false;
int row = board.size();
int col = board[0].size();
//using a mask to mark which char has been selected.
//do not use vector<bool>, it has big performance issue, could cause Time Limit Error
vector< vector<int> > mask(row, vector<int>(col, 0));
for(int i=0; i<board.size(); i++) {
for(int j=0; j<board[i].size(); j++){
if ( board[i][j]==word[0] ){
vector< vector<int> > m = mask;
if( exist(board, word, 0, i, j, m) ){
return true;
}
}
}
}
return false;
}
vector< vector<char> > buildBoard(char b[][5], int r, int c) {
vector< vector<char> > board;
for (int i=0; i<r; i++){
vector<char> v(b[i], b[i]+c);
cout << b[i] << endl;
board.push_back(v);
}
cout << "----------" << endl;
return board;
}
int main(int argc, char** argv)
{
string s;
char b[3][5] ={ "ABCE", "SFCS", "ADEE" };
vector< vector<char> > board = buildBoard(b, 3, 4);
s = "SEE";
cout << s << ":" << exist(board, s) << endl;
s = "ABCCED";
cout << s << ":" << exist(board, s) << endl;
s = "ABCB";
cout << s << ":" << exist(board, s) << endl;
if (argc>1){
s = argv[1];
cout << s << ":" << exist(board, s) << endl;
}
cout << endl << "----------" << endl;
char b1[3][5] ={ "CAA", "AAA", "BCD" };
board = buildBoard(b1, 3, 3);
s = "AAB";
cout << s << ":" << exist(board, s) << endl;
cout << endl << "----------" << endl;
char b2[3][5] ={ "ABCE", "SFES", "ADEE" };
board = buildBoard(b2, 3, 4);
s = "ABCESEEEFS";
cout << s << ":" << exist(board, s) << endl;
cout << endl << "----------" << endl;
char b3[3][5] ={ "aaaa", "aaaa", "aaaa" };
board = buildBoard(b3, 3, 4);
s = "aaaaaaaaaaaaa";
cout << s << ":" << exist(board, s) << endl;
return 0;
}