348. Design Tic-Tac-Toe

2022/4/14 6:12:44

本文主要是介绍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);
 */

 



这篇关于348. Design Tic-Tac-Toe的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程