-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordSearch.cpp
More file actions
116 lines (102 loc) · 3.16 KB
/
Copy pathWordSearch.cpp
File metadata and controls
116 lines (102 loc) · 3.16 KB
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
/*
* =====================================================================================
* Filename: WordSearch.cpp
*
* Description:
*
* Version: 1.0
* Created: 2015-03-06
* Revision: none
* Compiler: gcc
*
* Author: yixun
*
* =====================================================================================
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.
Hide Tags Array Backtracking
*/
#include <stdio.h>
#include <vector>
#include <string>
#include <set>
using namespace std;
class Solution {
private:
static int direct[4][2];
bool check(vector<vector<char> > &board, const string& word, vector<vector<bool> > &visit, int i, int j, int k) {
visit[i][j] = true;
if (board[i][j] == word[k]) {
if (k == word.length() - 1) {
return true;
}
for (int d = 0; d < 4; d++) {
int next_i = i + direct[d][0];
int next_j = j + direct[d][1];
if (next_i >= 0 && next_i < board.size()
&& next_j >= 0 && next_j < board[i].size()
&& !visit[next_i][next_j]) {
bool is_exist = check(board, word, visit, next_i, next_j, k+1);
if (is_exist) {
return true;
}
}
}
}
visit[i][j] = false;
return false;
}
public:
bool exist(vector<vector<char> > &board, string word) {
vector<vector<bool> > visit;
visit.resize(board.size());
for (int i = 0; i < visit.size(); i++) {
visit[i].resize(board[i].size(), false);
}
for (int i = 0; i < board.size(); i++) {
for (int j = 0; j < board[i].size(); j++) {
bool is_exist = check(board, word, visit, i, j, 0);
if (is_exist) {
return true;
}
}
}
return false;
}
};
int Solution::direct[4][2] = { 1, 0, -1, 0, 0, 1, 0, -1 };
int main() {
char board[3][4] = {
{'A', 'B', 'C', 'E'},
{'S', 'F', 'C', 'S'},
{'A', 'D', 'E', 'E'},
};
vector<vector<char> > board_vec;
for (int i = 0; i < 3; i++) {
vector<char> line;
for (int j = 0; j < 4; j++) {
line.push_back(board[i][j]);
}
board_vec.push_back(line);
}
Solution s;
string word = "ABCCED";
bool exist = s.exist(board_vec, word);
printf("str=%s, exist=%d\n", word.c_str(), exist);
word = "SEE";
exist = s.exist(board_vec, word);
printf("str=%s, exist=%d\n", word.c_str(), exist);
word = "ABCB";
exist = s.exist(board_vec, word);
printf("str=%s, exist=%d\n", word.c_str(), exist);
}