-
-
Notifications
You must be signed in to change notification settings - Fork 239
Expand file tree
/
Copy pathexample.cpp
More file actions
71 lines (61 loc) · 1.81 KB
/
example.cpp
File metadata and controls
71 lines (61 loc) · 1.81 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
#include <queue>
#include <string>
#include <vector>
#include "connect.h"
namespace connect {
static const int dx[6] = {-1, -1, 0, 0, 1, 1};
static const int dy[6] = {0, 1, -1, 1, 0, -1};
std::string winner(const std::vector<std::string>& board) {
int n = board.size();
if (n == 0) return "";
std::vector<std::vector<char>> grid(n);
for (int i = 0; i < n; ++i) {
for (char c : board[i]) {
if (c != ' ') {
grid[i].push_back(c);
}
}
}
int m = grid[0].size();
auto bfs = [&](char player) {
std::vector<std::vector<bool>> vis(n, std::vector<bool>(m, false));
std::queue<std::pair<int, int>> q;
if (player == 'X') {
for (int i = 0; i < n; ++i) {
if (grid[i][0] == 'X') {
q.push({i, 0});
vis[i][0] = true;
}
}
} else {
for (int j = 0; j < m; ++j) {
if (grid[0][j] == 'O') {
q.push({0, j});
vis[0][j] = true;
}
}
}
while (!q.empty()) {
auto [x, y] = q.front();
q.pop();
if ((player == 'X' && y == m - 1) ||
(player == 'O' && x == n - 1)) {
return true;
}
for (int d = 0; d < 6; ++d) {
int nx = x + dx[d];
int ny = y + dy[d];
if (nx >= 0 && nx < n && ny >= 0 && ny < m && !vis[nx][ny] &&
grid[nx][ny] == player) {
vis[nx][ny] = true;
q.push({nx, ny});
}
}
}
return false;
};
if (bfs('X')) return "X";
if (bfs('O')) return "O";
return "";
}
} // namespace connect