[Leetcode] 794. Valid Tic-Tac-Toe State 解题报告

题目

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’出现的次数x_count和‘O’出现的次数o_count,合法的棋盘应该是要么‘X’出现的次数等于‘O’出现的次数(最后一步由player2下出),要么‘X’出现的次数等于‘O’出现的次数加1(最后一步由player1下出)。如果都不符合这两种情况,那么就可以直接返回false了。

2)统计形成三个连续‘X’的个数x_lines,以及形成三个连续‘O’的个数o_lines。由于棋盘上只有9个位置,所以o_lines不可能等于2。然后我们对x_lins的出现次数进行分类讨论:a) x_lines == 2,此时只有可能是某一行(列)和某个对角线形成了两个x_liens,例如["XXX", "OXO", "OOX"],所以只需要判断x_count是否为5即可;b) x_lines == 1,此时必须o_lines == 0并且棋盘上‘X’的个数大于‘O’的个数才行,因为只有这样才能说明是player1最后一步下了一个‘X’导致获胜的;c) 如果x_lines == 0,那么要么o_lines == 0表示双方都没有获胜,要么players2获胜了,即o_lines == 1 && (x_count == o_count)。

代码

class Solution {
public:
    bool validTicTacToe(vector<string>& board) {
        // step 1: check the number of 'X's and 'O's
        int x_count = 0, o_count = 0;
        for (int r = 0; r < 3; ++r) {
            for (int c = 0; c < 3; ++c) {
                x_count += board[r][c] == 'X';
                o_count += board[r][c] == 'O';
            }
        }
        if (x_count != o_count && x_count != o_count + 1) {
            return false;
        }
        // step 2: check the number of 'X'lines and 'O' lines
        int x_lines = 0, o_lines = 0;
        const string &r1 = board[0], &r2 = board[1], &r3 = board[2];
        string c1, c2, c3, d1, d2;
        c1 += board[0][0], c1 += board[1][0], c1 += board[2][0];
        c2 += board[0][1], c2 += board[1][1], c2 += board[2][1];
        c3 += board[0][2], c3 += board[1][2], c3 += board[2][2];
        d1 += board[0][0], d1 += board[1][1], d1 += board[2][2];
        d2 += board[0][2], d2 += board[1][1], d2 += board[2][0];
        x_lines += r1 == "XXX", x_lines += r2 == "XXX", x_lines += r3 == "XXX";
        x_lines += c1 == "XXX", x_lines += c2 == "XXX", x_lines += c3 == "XXX";
        x_lines += d1 == "XXX", x_lines += d2 == "XXX";
        o_lines += r1 == "OOO", o_lines += r2 == "OOO", o_lines += r3 == "OOO";
        o_lines += c1 == "OOO", o_lines += c2 == "OOO", o_lines += c3 == "OOO";
        o_lines += d1 == "OOO", o_lines += d2 == "OOO";
        if (x_lines == 2) {
            return x_count == 5;
        }
        else if (x_lines == 1) {
            return o_lines == 0 && (x_count > o_count);
        }
        else {
            return o_lines == 0 || (o_lines == 1 && x_count == o_count);
        }
    }
};

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值