程序员面试金典 16.4

Tic Tac Win:根据给定的棋盘,判断井字游戏的输赢。

传统的井字游戏就是3 x 3的,所以一共3 ^ 9 = 19683种状态,存下来直接查就好了,但是力扣上是N x N的棋盘,所以这种方法就不适用了。

只要依次检查每一行、每一列还有两条对角线就可以了。

class Solution {
public:
    string tictactoe(vector<string>& board) {
        char first;
        size_t r, c;
        r = 0;
        for(r = 0; r < board.size(); r++)
        {
            first = board[r][0];
            if(first == ' ') continue;
            c = 1;
            while(c < board[r].size()){
                if(board[r][c] != first) break;
                c++;
            }
            if(c == board[r].size()) return string(1, first);
        }
        for(c = 0; c < board[0].size(); c++){
            first = board[0][c];
            if(first == ' ') continue;
            r = 1;
            while(r < board.size()){
                if(board[r][c] != first) break;
                r++;
            }
            if(r == board.size()) return string(1, first);;
        }
        r = 0, c = 0;
        first = board[r][c];
        if(first != ' '){
            while(r < board.size() && c < board[r].size()){
                if(board[r][c] != first) break;
                r++, c++;
            }
            if(r == board.size() && c == board[0].size()) return string(1, first);;
        }
        r = 0, c = board[0].size();
        first = board[r][c - 1];
        if(first != ' '){
            while(r < board.size() && c > 0){
                if(board[r][c - 1] != first) break;
                r++, c--;
            }
            if(r == board.size() && c == 0) return string(1, first);
        }
        r = 0;
        while(r < board.size()){
            c = 0;
            while(c < board[r].size()){
                if(board[r][c] == ' ') return "Pending";
                c++;
            }
            r++;
        }
        return "Draw";
    }
};

但是代码写起来很难看,大部分情况下都会选择复制前面的代码然后改改下标,这样非常容易出错,所以应该借鉴迭代器模式进行访问。

迭代器模式提供了一种访问聚合元素的方法,使用迭代器定义的next()运算就可以顺序访问下一个元素。Iterator类构造函数传入迭代器的起始坐标、坐标的递增方式和坐标的有效范围。

class Solution {
public:
    string tictactoe(vector<string>& board) {
        vector<Iterator> vecIter;
        for(size_t i = 0; i < board.size(); i++)
        {
            vecIter.push_back(Iterator(i, 0, 0, 1, board.size(), board[0].size()));
        }
        for(size_t j = 0; j < board[0].size(); j++)
        {
            vecIter.push_back(Iterator(0, j, 1, 0, board.size(), board[0].size()));
        }
        vecIter.push_back(Iterator(0, 0, 1, 1, board.size(), board[0].size()));
        vecIter.push_back(Iterator(0, board.size() - 1, 1, -1, board.size(), board[0].size()));
        for(Iterator iter : vecIter)
        {
            char first = board[iter.row][iter.col];
            if(first == ' ') continue;
            iter.next();
            while(!iter.end()){
                if(board[iter.row][iter.col] != first) break;
                iter.next();
            }
            if(iter.end()) return string(1, first);
        }
        for(size_t i = 0; i < board.size(); i++)
        {
            for(size_t j = 0; j < board[i].size(); j++)
            {
                if(board[i][j] == ' ') return "Pending";
            }
        }
        return "Draw";
    }
private:
    struct Iterator
    {
        int row, col;
        int rowInc, colInc;
        int rowBound, colBound;
        Iterator(int row, int col, int rowInc, int colInc, int rowBound, int colBound) : row(row), col(col), rowInc(rowInc), colInc(colInc), rowBound(rowBound), colBound(colBound){}
        void next()
        {
            row += rowInc;
            col += colInc;
        }
        bool end()
        {
            return !(0 <= row && row < rowBound && 0 <= col && col < colBound);
        }
    };
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值