*Leetcode 794. Valid Tic-Tac-Toe State | 规则

https://leetcode.com/problems/valid-tic-tac-toe-state/description/

本来以为需要搜,想了下 规则应该够 用于练习编程是否心细

class Solution {
public:
    
    int count( vector<string> & board, char c) {
        int ret = 0;
        for (int i = 0; i < board.size(); i++) {
            for (int j = 0; j < board[i].size(); j++) {
                if (c == board[i][j])
                    ret ++;
            }
        }
        return ret;
    }
    
    int wincnt( vector<string> &board, char c ) {
        int ret = 0;
        // row
        for (int i = 0; i < board.size(); i++) {
            bool check = true;
            for (int j = 0; j < board[i].size(); j++) {
                if (c != board[i][j]) {
                    check = false;
                    break;
                }
            }
            
            if (check)
                ret++;
            
        }
        // col
        for (int i = 0; i < board[0].size(); i++) {
            bool check = true;
            for (int j = 0; j < board.size(); j++) {
                if (board[j][i] != c) {
                    check = false;
                    break;
                }
            }
            if (check)
                ret++;
        }
        
        //
        bool check = true;
        for (int i = 0; i < board.size(); i++) 
            if (board[i][i] != c) {
                check = false;
                break;
            }
        if (check)
            ret++;
        
        if (board[0][2] == c && board[1][1] == c && board[2][0] == c ) {
            ret ++;
        }
        return ret;
        
    }
    
    bool validTicTacToe(vector<string>& board) {
        char x = 'X', o = 'O', sp = ' ';
        int xcnt = count(board, x);
        int ocnt = count(board, o);
        if (xcnt < ocnt || xcnt > ocnt + 1 ) return false;
        int owin = wincnt(board, o), xwin = wincnt(board, x);
        if (owin > 1 || (xwin && owin) ) return false;
        if (xwin && xcnt == ocnt) return false;
        if (owin && xcnt > ocnt) return false;
        return true;
    }
};



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值