其他分享
首页 > 其他分享> > 348. Design Tic-Tac-Toe

348. Design Tic-Tac-Toe

作者:互联网

class TicTacToe {
    int n;
    int[] rows;
    int[] cols;
    int diag=0;
    int antiDiag = 0;

    public TicTacToe(int n) {
        this.n = n;
        rows = new int[n];
        cols = new int[n];
    }
    
    public int move(int row, int col, int player) {
        int add = 1;
        if(player!=1)
            add = -1;
        rows[row]+=add;
        cols[col]+=add;
        if(row==col)
            diag+=add;
        if(row == n-col-1)
            antiDiag+=add;
        
        if(Math.abs(rows[row])==n||Math.abs(cols[col])==n||Math.abs(diag)==n||Math.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);
 */

 

标签:int,cols,add,Design,TicTacToe,Tac,Toe,col,row
来源: https://www.cnblogs.com/feiflytech/p/16142922.html