348. Design Tic-Tac-Toe

348. Design Tic-Tac-Toe


Design a Tic-tac-toe game that is played between two players on a n x n grid.

You may assume the following rules:

A move is guaranteed to be valid and is placed on an empty block.
Once a winning condition is reached, no more moves is allowed.
A player who succeeds in placing n of their marks in a horizontal, vertical, or diagonal row wins the game.
Example:
Given n = 3, assume that player 1 is “X” and player 2 is “O” in the board.

TicTacToe toe = new TicTacToe(3);

toe.move(0, 0, 1); -> Returns 0 (no one wins)
|X| | |
| | | |    // Player 1 makes a move at (0, 0).
| | | |

toe.move(0, 2, 2); -> Returns 0 (no one wins)
|X| |O|
| | | |    // Player 2 makes a move at (0, 2).
| | | |

toe.move(2, 2, 1); -> Returns 0 (no one wins)
|X| |O|
| | | |    // Player 1 makes a move at (2, 2).
| | |X|

toe.move(1, 1, 2); -> Returns 0 (no one wins)
|X| |O|
| |O| |    // Player 2 makes a move at (1, 1).
| | |X|

toe.move(2, 0, 1); -> Returns 0 (no one wins)
|X| |O|
| |O| |    // Player 1 makes a move at (2, 0).
|X| |X|

toe.move(1, 0, 2); -> Returns 0 (no one wins)
|X| |O|
|O|O| |    // Player 2 makes a move at (1, 0).
|X| |X|

toe.move(2, 1, 1); -> Returns 1 (player 1 wins)
|X| |O|
|O|O| |    // Player 1 makes a move at (2, 1).
|X|X|X|

Follow up:
Could you do better than O(n2) per move() operation?
Hint:

  1. Could you trade extra space such that move() operation can be done in O(1)?
  2. You need two arrays: int rows[n], int cols[n], plus two variables: diagonal, anti_diagonal.

方法1:

思路:

用n * n 的空间来存储棋盘,player1放入1,player2放入2,如果没有标记为0。每次放入之后检查行、列、对角线、反对角线是否已经都被该player占据。

Complexity

Time complexity: O(n)
Space complexity: O(n^2)

class TicTacToe {
public:
    /** Initialize your data structure here. */
    TicTacToe(int n) {
        board.resize(n, vector<int>(n, 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) {
        board[row][col] = player;
        int n = board.size();
        int i = 0;
        for (; i < n; i++){
            if (board[row][i] != player) break;
        }
        if (i == n) return player;
        
        i = 0;
        for (; i < n; i++){
            if (board[i][col] != player) break;
        }
        if (i == n) return player;
        
        // 正对角线
        if (row == col){
            i = 0;
            for (; i < n; i++){
                if (board[i][i] != player) break;
            }
            if (i == n) return player;
        }
        
        // 反对角线
        if (row + col == n - 1){   
            i = 0;
            for (; i < n; i++){
                if (board[i][n - 1 - i] != player) break;
            }
            if (i == n) return player;
        }
        return 0;
    }
private: 
    vector<vector<int>> board;
};

/**
 * Your TicTacToe object will be instantiated and called as such:
 * TicTacToe* obj = new TicTacToe(n);
 * int param_1 = obj->move(row,col,player);
 */

方法2:

思路:

Follow up中让我们用更高效的方法。根据hint,用 row[n], col[n],diag, anti diag,来记录棋盘。因为赢的条件必须是同一个玩家占据了n个格子,那么如果每次player1放子,在相应的位置上++,player2放子,在相应的位置上–,唯一赢得比赛的条件就是某一个方向出现了n或者-n。那么每次在O(n)的时间内就能够判断输赢。用到的小trick就是由player - 1 为0或1来判断其获胜的条件goal是什么,n或者-n,如果上面四种情况任意一种被满足,都可以返回player,否则返回0。

Complexity

Time complexity: O(1)
Space complexity: O(n)

class TicTacToe {
public:
    /** Initialize your data structure here. */
    TicTacToe(int n) {
        _row.resize(n);
        _col.resize(n);
        diag = 0;
        anti_diag = 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 n = _row.size();
        int val = (player - 1) ? 1 : -1;
        int goal = n * val;
        _row[row] += val;
        _col[col] += val;
        if (row == col) diag += val;
        if (row + col == n - 1) anti_diag += val;
        
        return (_row[row] == goal || _col[col] == goal || diag == goal || anti_diag == goal) ? player : 0;
    }
private:
    vector<int> _row;
    vector<int> _col;
    int diag;
    int anti_diag;
};

/**
 * Your TicTacToe object will be instantiated and called as such:
 * TicTacToe* obj = new TicTacToe(n);
 * int param_1 = obj->move(row,col,player);
 */
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值