Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class TicTacToe {
- public:
- vector<int> rows;
- vector<int> cols;
- int diag, antidiag;
- /** Initialize your data structure here. */
- TicTacToe(int n) {
- rows.assign(n, 0);
- cols.assign(n, 0);
- diag = antidiag = 0;
- }
- /** 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) {
- int cur = (player == 1) ? 1: -1;
- rows[row] += cur;
- cols[col] += cur;
- int n = rows.size();
- if(row == col) diag += cur;
- if(row+col == n-1) antidiag += cur;
- if(abs(rows[row]) == n || abs(cols[col]) == n || abs(diag) == n || abs(antidiag) == n){
- return player;
- }
- 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);
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement