forked from rost0413/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWord_Search.cpp
99 lines (87 loc) · 2.11 KB
/
Word_Search.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
/*
Author: Weixian Zhou, [email protected]
Date: Jul 27, 2012
Problem: Word Search
Difficulty: easy
Source: http://www.leetcode.com/onlinejudge
Notes:
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.
Solution:
When given this problem, I have two solutions instantly.
1) construct a tree simliar to trie.
2) DFS.
The tree construction solution will get a very big tree and not so simple.
The DFS method is simple to implement, although not so fast.
*/
#include <vector>
#include <set>
#include <climits>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstring>
using namespace std;
class Solution {
public:
int m;
int n;
bool valid(int &nx, int &ny, int dir, vector<vector<char> > &board, char c) {
if (dir == 0) { nx--; }
else if (dir == 1) { nx++; }
else if (dir == 2) { ny--; }
else { ny++; }
if (0 <= nx && nx < m && 0 <= ny && ny < n && board[nx][ny] == c) {
return true;
}
return false;
}
bool dfs(vector<vector<char> > &board, string word, int pos, int x, int y) {
if (pos >= word.length()) {
return true;
}
for (int i = 0, nx, ny; i < 4; i++) {
nx = x, ny = y;
if (valid(nx, ny, i, board, word[pos])) {
char c = board[nx][ny];
board[nx][ny] = '.';
if (dfs(board, word, pos + 1, nx, ny)) {
return true;
}
board[nx][ny] = c;
}
}
return false;
}
bool exist(vector<vector<char> > &board, string word) {
m = board.size();
n = board[0].size();
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (board[i][j] == word[0]) {
char c = board[i][j];
board[i][j] = '.';
if (dfs(board, word, 1, i, j)) {
return true;
}
board[i][j] = c;
}
}
}
return false;
}
}
;