Skip to content

Commit 8335b2b

Browse files
committed
Add solutions for leetcode 73.
1 parent 700d8dc commit 8335b2b

4 files changed

+241
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#include <vector>
2+
#include <iostream>
3+
using namespace std;
4+
5+
class Solution {
6+
public:
7+
bool exist(vector<vector<char>>& board, string word) {
8+
int m = board.size();
9+
int n = board[0].size();
10+
vector<vector<int>> visited(m, vector<int>(n, 0));
11+
for (int i = 0; i < m; i++)
12+
{
13+
for (int j = 0; j < n; j++)
14+
{
15+
if (dfs(board, i, j, visited, word, 0))
16+
return true;
17+
}
18+
}
19+
return false;
20+
}
21+
// 是否能找到index = curPos处的字符, curPos: 在目标单词中的index
22+
bool dfs(vector<vector<char>>& board, int x, int y, vector<vector<int>>& visited, string word, int curPos)
23+
{
24+
if (curPos == word.size()) return true;
25+
int m = board.size();
26+
int n = board[0].size();
27+
if (x < 0 || x >= m || y < 0 || y >= n) return false;
28+
if (visited[x][y] == 1) return false;
29+
if (board[x][y] != word[curPos]) return false;
30+
vector<pair<int, int>> dirs({{1, 0}, {-1, 0}, {0, 1}, {0, -1}}); // 右/左/上/下
31+
visited[x][y] = 1;
32+
for (auto dir : dirs)
33+
{
34+
auto dirX = dir.first;
35+
auto dirY = dir.second;
36+
if (dfs(board, x + dirX, y + dirY, visited, word, curPos + 1)) return true;
37+
}
38+
visited[x][y] = 0;
39+
40+
return false;
41+
}
42+
};
43+
44+
// Test
45+
int main()
46+
{
47+
Solution sol;
48+
vector<vector<char>> board
49+
{
50+
{'A', 'B', 'C', 'E'},
51+
{'S', 'F', 'C', 'S'},
52+
{'A', 'D', 'E', 'E'}
53+
};
54+
string word = "ABCCED";
55+
56+
bool isValid = sol.exist(board, word);
57+
cout << (isValid == true ? "true" : "false") << endl;
58+
59+
return 0;
60+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#include <vector>
2+
#include <iostream>
3+
using namespace std;
4+
5+
class Solution {
6+
public:
7+
bool exist(vector<vector<char>>& board, string word) {
8+
int m = board.size();
9+
int n = board[0].size();
10+
vector<vector<int>> visited(m, vector<int>(n, 0));
11+
for (int i = 0; i < m; i++)
12+
{
13+
for (int j = 0; j < n; j++)
14+
{
15+
if (dfs(board, i, j, visited, word, 0))
16+
return true;
17+
}
18+
}
19+
return false;
20+
}
21+
// 是否能找到index = curPos处的字符, curPos: 在目标单词中的index
22+
bool dfs(vector<vector<char>>& board, int x, int y, vector<vector<int>>& visited, string& word, int curPos)
23+
{
24+
if (curPos == word.size()) return true;
25+
int m = board.size();
26+
int n = board[0].size();
27+
if (x < 0 || x >= m || y < 0 || y >= n) return false;
28+
if (visited[x][y] == 1) return false;
29+
if (board[x][y] != word[curPos]) return false;
30+
31+
int dx[4] = {-1, 1, 0, 0}, dy[4] = {0, 0, 1, -1}; /* 方向变量不使用vector<pair>, 速度会变快, 依次表示右/左/上/下 */
32+
visited[x][y] = 1;
33+
for (int i = 0; i < 4; i++)
34+
{
35+
if (dfs(board, x + dx[i], y + dy[i], visited, word, curPos + 1)) return true;
36+
}
37+
visited[x][y] = 0;
38+
39+
return false;
40+
}
41+
};
42+
43+
// Test
44+
int main()
45+
{
46+
Solution sol;
47+
vector<vector<char>> board
48+
{
49+
{'A', 'B', 'C', 'E'},
50+
{'S', 'F', 'C', 'S'},
51+
{'A', 'D', 'E', 'E'}
52+
};
53+
string word = "ABCCED";
54+
55+
bool isValid = sol.exist(board, word);
56+
cout << (isValid == true ? "true" : "false") << endl;
57+
58+
return 0;
59+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#include <vector>
2+
#include <iostream>
3+
using namespace std;
4+
5+
class Solution {
6+
vector<vector<char>> M;
7+
string str;
8+
public:
9+
bool exist(vector<vector<char>>& board, string word) {
10+
M = board;
11+
str = word;
12+
int m = M.size();
13+
int n = M[0].size();
14+
vector<vector<int>> visited(m, vector<int>(n, 0));
15+
for (int i = 0; i < m; i++)
16+
{
17+
for (int j = 0; j < n; j++)
18+
{
19+
if (dfs(i, j, visited, 0))
20+
return true;
21+
}
22+
}
23+
return false;
24+
}
25+
bool dfs(int x, int y, vector<vector<int>>& visited, int curPos)
26+
{
27+
if (curPos == str.size()) return true;
28+
int m = M.size();
29+
int n = M[0].size();
30+
if (x < 0 || x >= m || y < 0 || y >= n) return false;
31+
if (visited[x][y] == 1) return false;
32+
if (M[x][y] != str[curPos]) return false;
33+
vector<pair<int, int>> dirs = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; // 右/左/上/下
34+
visited[x][y] = 1;
35+
for (auto dir : dirs)
36+
{
37+
auto dirX = dir.first;
38+
auto dirY = dir.second;
39+
if (dfs(x + dirX, y + dirY, visited, curPos + 1)) return true;
40+
}
41+
visited[x][y] = 0;
42+
43+
return false;
44+
}
45+
};
46+
47+
// Test
48+
int main()
49+
{
50+
Solution sol;
51+
vector<vector<char>> board
52+
{
53+
{'A', 'B', 'C', 'E'},
54+
{'S', 'F', 'C', 'S'},
55+
{'A', 'D', 'E', 'E'}
56+
};
57+
string word = "ABCCED";
58+
59+
bool isValid = sol.exist(board, word);
60+
cout << (isValid == true ? "true" : "false") << endl;
61+
62+
return 0;
63+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#include <vector>
2+
#include <iostream>
3+
using namespace std;
4+
5+
class Solution {
6+
public:
7+
bool exist(vector<vector<char>>& board, string word) {
8+
int m = board.size();
9+
int n = board[0].size();
10+
for (int i = 0; i < m; i++)
11+
{
12+
for (int j = 0; j < n; j++)
13+
{
14+
if (dfs(board, i, j, word, 0))
15+
return true;
16+
}
17+
}
18+
return false;
19+
}
20+
21+
bool dfs(vector<vector<char>>& board, int x, int y, string& str, int curPos)
22+
{
23+
if (board[x][y] != str[curPos]) return false;
24+
if (curPos == str.size() - 1) return true;
25+
int dx[4] = {-1, 1, 0, 0}, dy[4] = {0, 0, 1, -1}; /* 推荐写法: 使用两个定长数组做方向向量, 右/左/上/下 */
26+
char ch = board[x][y];
27+
board[x][y] = '*'; /* word中只含有英文字母, 这里用英文字母以外的字符都行, 比如使用星号 */
28+
for (int i = 0; i < 4; i++)
29+
{
30+
int newX = x + dx[i], newY = y + dy[i];
31+
if (newX >= 0 && newX < board.size() && newY >= 0 && newY < board[0].size())
32+
{
33+
if (dfs(board, newX, newY, str, curPos + 1))
34+
return true;
35+
}
36+
}
37+
board[x][y] = ch;
38+
39+
return false;
40+
}
41+
};
42+
43+
// Test
44+
int main()
45+
{
46+
Solution sol;
47+
vector<vector<char>> board
48+
{
49+
{'A', 'B', 'C', 'E'},
50+
{'S', 'F', 'C', 'S'},
51+
{'A', 'D', 'E', 'E'}
52+
};
53+
string word = "ABCCED";
54+
55+
bool isValid = sol.exist(board, word);
56+
cout << (isValid == true ? "true" : "false") << endl;
57+
58+
return 0;
59+
}

0 commit comments

Comments
 (0)