[leetcode] 794. Valid Tic-Tac-Toe State

441 篇文章 0 订阅
284 篇文章 0 订阅

Description

A Tic-Tac-Toe board is given as a string array board. Return True if and only if it is possible to reach this board position during the course of a valid tic-tac-toe game.

The board is a 3 x 3 array, and consists of characters " ", “X”, and “O”. The " " character represents an empty square.

Here are the rules of Tic-Tac-Toe:

Players take turns placing characters into empty squares (" ").
The first player always places “X” characters, while the second player always places “O” characters.
“X” and “O” characters are always placed into empty squares, never filled ones.
The game ends when there are 3 of the same (non-empty) character filling any row, column, or diagonal.
The game also ends if all squares are non-empty.
No more moves can be played if the game is over.
Example 1:
Input:

board = ["O  ", "   ", "   "]

Output:

false

Explanation:

The first player always plays "X".

Example 2:
Input:

board = ["XOX", " X ", "   "]

Output:

false

Explanation:

Players take turns making moves.

Example 3:
Input:

board = ["XXX", "   ", "OOO"]

Output:

false

Example 4:
Input:

board = ["XOX", "O O", "XOX"]

Output:

true

Note:

  • board is a length-3 array of strings, where each string board[i] has length 3.
  • Each board[i][j] is a character in the set {" ", “X”, “O”}.

分析

题目的意思是:验证#字棋的棋盘状态是否合法。

  1. 由规则可知,”X”一定最先开始,所以当前局面存在”O”的个数大于”X”的个数为非法。
  2. 其次,由于”X”和”O”轮流,因此,当前局面中”X”的个数要么和”O”相等,要么比”O”多一。
  3. “O”在当前局面赢得比赛的情况下,上一轮的”X”一定不能赢得局面。
  4. “O”在当前局面赢得比赛的情况下,上一轮的”X”没有赢得局面时,合法局面必须满足”O”的个数等于”X”的个数。
  5. “X”在当前局面赢得比赛的情况下,意味着上一轮”O”没有赢得局面,合法局面下,”X”的个数正好比”O”的个数多一。

代码

class Solution {
public:
    bool validTicTacToe(vector<string>& board) {
        int cx=cnt(board,'X'), co=cnt(board,'O');
        bool wx=win(board,'X'), wo=win(board,'O');
        if(wx&&wo) return false;
        if(wx) return cx==co+1;
        if(wo) return cx==co;
        return cx==co+1||co==cx;
    }
    int cnt(vector<string> b,char c){
        int res=0;
        for(string s:b){
            res+=count(s.begin(),s.end(),c);
        }
        return res;
    }
    bool win(vector<string> b, char c){
        for(int i=0;i<3;i++){
            if(b[i][0]==c&&b[i][1]==c&&b[i][2]==c){
                return true;
            }
        }
        for(int j=0;j<3;j++){
            if(b[0][j]==c&&b[1][j]==c&&b[2][j]==c){
                return true;
            }
        }
        return (b[0][0]==c&&b[1][1]==c&&b[2][2]==c)||(b[0][2]==c&&b[1][1]==c&&b[2][0]==c);
    }
};

参考文献

794. Valid Tic-Tac-Toe State
LWC 74: 794. Valid Tic-Tac-Toe State

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

农民小飞侠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值