forked from lzl124631x/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
s1.cpp
45 lines (45 loc) · 1.21 KB
/
s1.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
// OJ: https://leetcode.com/problems/design-tic-tac-toe/
// Author: github.com/lzl124631x
// Time: O(N)
// Space: O(N^2)
class TicTacToe {
private:
vector<vector<int>> board;
int n;
int status(int row, int col) {
int p = board[row][col];
bool win = true;
for (int i = 0; i < n && win; ++i) {
if (board[row][i] != p) win = false;
}
if (win) return p;
win = true;
for (int i = 0; i < n && win; ++i) {
if (board[i][col] != p) win = false;
}
if (win) return p;
if (row == col) {
win = true;
for (int i = 0; i < n && win; ++i) {
if (board[i][i] != p) win = false;
}
if (win) return p;
}
if (row + col == n - 1) {
win = true;
for (int i = 0; i < n && win; ++i) {
if (board[i][n - 1 - i] != p) win = false;
}
if (win) return p;
}
return 0;
}
public:
TicTacToe(int n) : n(n) {
board = vector<vector<int>>(n, vector<int>(n));
}
int move(int row, int col, int player) {
board[row][col] = player;
return status(row, col);
}
};