码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode "Design Tic-Tac-Toe"

时间:2016-05-07 14:59:42      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:

We don‘t have to keep a complete chess board.. just counters!

class TicTacToe {
    vector<int> cntVer;
    vector<int> cntHor;
    int cntDiag0;
    int cntDiag1;
    int _n;
public:
    /** Initialize your data structure here. */
    TicTacToe(int n) {
        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. */
    int move(int row, int col, int player) 
    {
        int d = player == 1 ? 1 : -1;
        
        cntVer[col] += d;
        if(abs(cntVer[col]) == _n) return player;
            
        cntHor[row] += d;
        if(abs(cntHor[row]) == _n) return player;
        
        if(col== row)
        {
            cntDiag0 += d;
            if(abs(cntDiag0) == _n) return player;
        }
        if ((col + row) == _n - 1)
        {
            cntDiag1 += d;
            if(abs(cntDiag1) == _n) return player;
        }
            
        return 0;
    }
};

LeetCode "Design Tic-Tac-Toe"

标签:

原文地址:http://www.cnblogs.com/tonix/p/5468270.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!