-
-
Notifications
You must be signed in to change notification settings - Fork 300
/
348.cpp
57 lines (53 loc) · 1.83 KB
/
348.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
__________________________________________________________________________________________________
class TicTacToe {
public:
/** Initialize your data structure here. */
TicTacToe(int n) {
board.resize(n, vector<int>(n, 0));
}
int move(int row, int col, int player) {
board[row][col] = player;
int i = 0, j = 0, n = board.size();
for (j = 1; j < n; ++j) {
if (board[row][j] != board[row][j - 1]) break;
}
if (j == n) return player;
for (i = 1; i < n; ++i) {
if (board[i][col] != board[i - 1][col]) break;
}
if (i == n) return player;
if (row == col) {
for (i = 1; i < n; ++i) {
if (board[i][i] != board[i - 1][i - 1]) break;
}
if (i == n) return player;
}
if (row + col == n - 1) {
for (i = 1; i < n; ++i) {
if (board[n - i - 1][i] != board[n - i][i - 1]) break;
}
if (i == n) return player;
}
return 0;
}
private:
vector<vector<int>> board;
};
__________________________________________________________________________________________________
class TicTacToe {
public:
/** Initialize your data structure here. */
TicTacToe(int n): rows(n), cols(n), N(n), diag(0), rev_diag(0) {}
int move(int row, int col, int player) {
int add = player == 1 ? 1 : -1;
rows[row] += add;
cols[col] += add;
diag += (row == col ? add : 0);
rev_diag += (row == N - col - 1 ? add : 0);
return (abs(rows[row]) == N || abs(cols[col]) == N || abs(diag) == N || abs(rev_diag) == N) ? player : 0;
}
private:
vector<int> rows, cols;
int diag, rev_diag, N;
};
__________________________________________________________________________________________________