-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathdesign_tic_tac_toe.cpp
More file actions
64 lines (58 loc) · 1.76 KB
/
Copy pathdesign_tic_tac_toe.cpp
File metadata and controls
64 lines (58 loc) · 1.76 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
// https://leetcode.com/problems/design-tic-tac-toe/
#include <vector>
class TicTacToe {
public:
/** Initialize your data structure here. */
int n_;
vector<int> diagonal_1{0,0};
vector<int> diagonal_2{0,0};
vector<vector<int>> rows;
vector<vector<int>> cols;
TicTacToe(int n) {
n_ = n;
for (int i = 0; i < 2; i++) {
vector<int> arr;
for (int j = 0; j < n; j++){
arr.push_back(0);
}
rows.push_back(arr);
}
for (int i = 0; i < 2; i++) {
vector<int> arr;
for (int j = 0; j < n; j++){
arr.push_back(0);
}
cols.push_back(arr);
}
}
/** Player {player} makes a move at ({row}, {col}).
@param row The row of the board.
@param col The column of the board.
@param player The player, can be either 1 or 2.
@return The current winning condition, can be either:
0: No one wins.
1: Player 1 wins.
2: Player 2 wins. */
int move(int row, int col, int player) {
if (row == col){
diagonal_1[player-1] += 1;
}
if (col == n_ - 1 - row){
diagonal_2[player-1] += 1;
}
rows[player-1][row] += 1;
cols[player-1][col] += 1;
if( (rows[player-1][row] == n_) or\ // dsfd
(cols[player-1][col] == n_) or (diagonal_1[player-1] == n_) or (diagonal_2[player-1] == n_) ){
return player;
}
else {
return 0;
}
}
};
/**
* Your TicTacToe object will be instantiated and called as such:
* TicTacToe obj = new TicTacToe(n);
* int param_1 = obj.move(row,col,player);
*/