forked from daiwb/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexist.cpp
More file actions
96 lines (92 loc) · 2.36 KB
/
exist.cpp
File metadata and controls
96 lines (92 loc) · 2.36 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
#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <string>
#include <cstring>
using namespace std;
#define REP(i,n) for(int i=0;i<(n);++i)
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define RFOR(i,a,b) for(int i=(a);i>=(b);--i)
#define FOREACH(it,c) for(typeof((c).begin())it=(c).begin();it!=(c).end();++it)
#define CLR(x) memset((x),0,sizeof((x)))
#define MP make_pair
#define MPI make_pair<int, int>
#define PB push_back
typedef long long LL;
typedef vector<int> VI;
typedef vector<string> VS;
typedef pair<int, int> PI;
int dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
int row, col, len;
vector<VI> vv;
bool valid;
vector<vector<char> > board;
string word;
class Solution {
public:
void dfs(int x, int y, int idx) {
if (valid) return;
if (idx == len) {
valid = true;
return;
}
REP(i,4) {
int tx = x + dir[i][0], ty = y + dir[i][1];
if (tx < 0 || tx >= row || ty < 0 || ty >= col) continue;
if (board[tx][ty] != word[idx]) continue;
if (vv[tx][ty] == 1) continue;
vv[tx][ty] = 1;
dfs(tx, ty, idx + 1);
vv[tx][ty] = 0;
}
}
bool isok(int x, int y) {
vv.assign(row, VI(col, 0));
valid = false;
vv[x][y] = 1;
dfs(x, y, 1);
return valid;
}
bool exist(vector<vector<char> >& _board, string _word) {
board = _board;
word = _word;
row = board.size();
if (row == 0) return false;
col = board[0].size();
if (col == 0) return false;
len = word.length();
if (len == 0) return false;
REP(i,row) {
REP(j,col) {
if (board[i][j] != word[0]) continue;
if (isok(i, j)) return true;
}
}
return false;
}
};
int main() {
Solution s = Solution();
vector<vector<char> > board;
string t[] = {"ABCE", "SFCS", "ADEE"};
REP(i,3) {
vector<char> q;
REP(j,4) {
q.PB(t[i][j]);
}
board.PB(q);
}
//cout << s.exist(board, "ABCCED") << endl;
//cout << s.exist(board, "SEE") << endl;
cout << s.exist(board, "ABCB") << endl;
return 0;
}