JobHopper-TicTocToe

基础版本:

class TicTacToe(object):

    def __init__(self, n):
        """
        Initialize your data structure here.
        :type n: int
        """
        self.board = [[0 for _ in range(n)] for _ in range(n)]
        self.n = n

    def move(self, row, col, player):
        """
        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.
        :type row: int
        :type col: int
        :type player: int
        :rtype: int
        """
        
        # I forget this step!! 
        self.board[row][col] = player
        
        # basically, once a move is placed, check 4 possibilities of winning the game.
        
        # checking the row, horizontally
        winning = True
        for j in range(self.n):
            if self.board[row][j] != player:
                winning = False
                break
        if winning: 
            return player

        # checking the col, vertically
        winning = True
        for i in range(self.n):
            if self.board[i][col] != player:
                winning = False
                break
        if winning: 
            return player

        # checking the diagnol
        winning = True
        if row == col:
            for i in range(self.n):
                if self.board[i][i] != player:  # Bug: self.baord[i][j]!! should be both index = i
                    winning = False
                    break
            if winning: 
                return player
        
        # checking the anti diagnal
        winning = True
        if row + col == self.n-1:
            for i in range(self.n):
                if self.board[i][self.n-1-i] != player:  # Bug: self.baord[i][self.n-1-j]!! should be both index should related to i
                    winning = False
                    break
            if winning: 
                return player
        
        return 0
        


# Your TicTacToe object will be instantiated and called as such:
# obj = TicTacToe(n)
# param_1 = obj.move(row,col,player)

优化版本,线性空间来存储信息。原因,题目明确说了 每一步都是 guaranteed to be valid,所以就 不需要 用 2D arary 来记录 board了

class TicTocToe(object):
    def __init__(self, n: int):
        self.row, self.col, self.diag, self.antidiag, self.n = [0] * n, [0] * n, 0, 0, n

    def move(self, row: int, col: int, player: int) -> int:
        # transform player id to -1/1, which will be easy to do result checking
        # player 1 --> 1*2 - 3 = -1
        # player 2 --> 2*2 - 3 = 1
        offset = player * 2 - 3

        self.row[row] += offset
        self.col[col] += offset
        if row == col:
            self.diag += offset
        # Bug: the move in the center of the board would satisy both diag and antidiag conditions
        # elif row + col == self.n-1:
        if row + col == self.n - 1:
            self.antidiag += offset

        # nice trick to do multiple checking
        if self.n in (self.row[row], self.col[col], self.diag, self.antidiag):
            # The player can play consecutive moves
            # it is not neccessarily following the order of {player1, player2, player1, player2} <-- some test cases
            return player
        # Bug: I forgot this checking!!
        elif -self.n in (self.row[row], self.col[col], self.diag, self.antidiag):
            return player
        else:
            return 0


def main():
    # test1
    game = TicTocToe(3)
    assert(game.move(0, 0, 1), 0)
    assert(game.move(0, 2, 2), 0)
    assert(game.move(2, 2, 1), 0)
    assert(game.move(1, 1, 2), 0)
    assert(game.move(2, 0, 1), 0)
    assert(game.move(1, 0, 2), 0)
    assert(game.move(2, 1, 1), 1)


if __name__ == "__main__":
    main()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值