-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path79.cpp
105 lines (92 loc) · 3.14 KB
/
79.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
__________________________________________________________________________________________________
12ms
static const int _ = []() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout.tie(nullptr);
return 0;
}();
class Solution {
public:
bool exist(std::vector<std::vector<char>>& Board, std::string Word) {
const int WordLen = Word.length();
if (WordLen < 1)
return true;
const int RowNum = Board.size();
if (RowNum < 1)
return false;
const int ColNum = Board[0].size();
if (ColNum < 1)
return false;
std::function<bool(int,int,int)> BackTrack;
BackTrack = [&](int Row, int Col, int CharIdx) {
if (CharIdx >= WordLen)
return true;
const char &WordCh = Word[CharIdx];
char &BoardCh = Board[Row][Col];
BoardCh = ~BoardCh;
bool IsMatch = false;
if (Row > 0 &&
Board[Row - 1][Col] == WordCh &&
BackTrack(Row - 1, Col, CharIdx + 1))
IsMatch = true;
else if (Col > 0 &&
Board[Row][Col - 1] == WordCh &&
BackTrack(Row, Col - 1, CharIdx + 1))
IsMatch = true;
else if (Row < RowNum - 1 &&
Board[Row + 1][Col] == WordCh &&
BackTrack(Row + 1, Col, CharIdx + 1))
IsMatch = true;
else if (Col < ColNum - 1 &&
Board[Row][Col + 1] == WordCh &&
BackTrack(Row, Col + 1, CharIdx + 1))
IsMatch = true;
BoardCh = ~BoardCh;
return IsMatch;
};
for (int Row = 0; Row < RowNum; ++Row) {
for (int Col = 0; Col < ColNum; ++Col) {
if (Board[Row][Col] == Word[0] && BackTrack(Row, Col, 1))
return true;
}
}
return false;
}
};
__________________________________________________________________________________________________
10004 kb
class Solution {
public:
int dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
bool exist(vector<vector<char>>& board, string word) {
for(int i = 0; i < board.size(); i++)
for(int j = 0; j < board[0].size(); j++)
if(board[i][j] == word[0] && helper(i, j, 0, board, word)) return 1;
// if(helper(i, j, 0, board, word)) return 1;
return 0;
}
bool helper(int i, int j, int w, vector<vector<char>> &board, string &word)
{
if(++w == word.size()) return 1;
// char c = board[i][j];
// board[i][j] = 0;
board[i][j] ^= -1;
for(auto &t : dir)
{
int a = i + t[0], b = j + t[1];
if(a >= 0 && b >= 0 && a < board.size() && b < board[0].size() && board[a][b] == word[w])
if(helper(a, b, w, board, word)) return 1;;
}
// board[i][j] = c;
board[i][j] ^= -1;
return 0;
}
};
static int x = []() {
std::ios::sync_with_stdio(false);
std::cin.tie(NULL);
std::cout.tie(NULL);
return NULL;
}();
__________________________________________________________________________________________________