We don ' t have the keep a complete chess board. Just counters!
classTicTacToe {vector<int>Cntver; Vector<int>Cnthor; intcntDiag0; intCntDiag1; int_n; Public: /** Initialize your data structure here.*/TicTacToe (intN) {cntver.assign (n,0); Cnthor.assign (N,0); CntDiag0= CntDiag1 =0; _n=N; } /** 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. */ intMoveintRowintColintplayer) { intd = Player = =1?1: -1; Cntver[col]+=D; if(ABS (Cntver[col]) = = _n)returnplayer; Cnthor[row]+=D; if(ABS (Cnthor[row]) = = _n)returnplayer; if(col==row) {cntDiag0+=D; if(ABS (CNTDIAG0) = = _n)returnplayer; } if(col + row) = = _n-1) {CntDiag1+=D; if(ABS (CNTDIAG1) = = _n)returnplayer; } return 0; }};
Leetcode "Design tic-tac-toe"