[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.html

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

Here are the rules of Tic-Tac-Toe:github

  • 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:this

  • 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"}.

 

这道题又是关于井字棋游戏的,以前也有一道相似的题 Design Tic-Tac-Toe,不过那道题是模拟游戏进行的,而这道题是让验证当前井字棋的游戏状态是否正确。这题的例子给的比较好,cover 了不少种状况:spa

 

状况一:code

0 _ _ _ _ _ _ _ _

 

这是不正确的状态,由于先走的使用X,因此只出现一个O,是不对的。htm

 

状况二:blog

X O X _ X _ _ _ _

 

这个也是不正确的,由于两个 player 交替下棋,X最多只能比O多一个,这里多了两个,确定是不对的。游戏

 

状况三:ci

X X X _ _ _ O O O

 

这个也是不正确的,由于一旦第一个玩家的X连成了三个,那么游戏立刻结束了,不会有另一个O出现。

 

状况四:

X O X O _ O X O X

 

这个状态没什么问题,是能够出现的状态。

好,那么根据给的这些例子,能够分析一下规律,根据例子1和例子2得出下棋顺序是有规律的,必须是先X后O,不能破坏这个顺序,那么可使用一个 turns 变量,当是X时,turns 自增1,反之如果O,则 turns 自减1,那么最终 turns 必定是0或者1,其余任何值都是错误的,好比例子1中,turns 就是 -1,例子2中,turns 是2,都是不对的。根据例子3,能够得出结论,只能有一个玩家获胜,能够用两个变量 xwin 和 owin,来记录两个玩家的获胜状态,因为井字棋的制胜规则是横竖斜任意一个方向有三个连续的就算赢,那么分别在各个方向查找3个连续的X,有的话 xwin 赋值为 true,还要查找3个连续的O,有的话 owin 赋值为 true,例子3中 xwin 和 owin 同时为 true 了,是错误的。还有一种状况,例子中没有 cover 到的是:

 

状况五:

X X X O O _ O _ _

 

这里虽然只有 xwin 为 true,可是这种状态仍是错误的,由于一旦第三个X放下后,游戏当即结束,不会有第三个O放下,这么检验这种状况呢?这时 turns 变量就很是的重要了,当第三个O放下后,turns 自减1,此时 turns 为0了,而正确的应该是当 xwin 为 true 的时候,第三个O不能放下,那么 turns 不减1,则仍是1,这样就能够区分状况五了。固然,能够交换X和O的位置,即当 owin 为 true 时,turns 必定要为0。如今已经覆盖了搜索的状况了,参见代码以下:

 

class Solution { public: bool validTicTacToe(vector<string>& board) { bool xwin = false, owin = false; vector<int> row(3), col(3); int diag = 0, antidiag = 0, turns = 0; for (int i = 0; i < 3; ++i) { for (int j = 0; j < 3; ++j) { if (board[i][j] == 'X') { ++row[i]; ++col[j]; ++turns; if (i == j) ++diag; if (i + j == 2) ++antidiag; } else if (board[i][j] == 'O') { --row[i]; --col[j]; --turns; if (i == j) --diag; if (i + j == 2) --antidiag; } } } xwin = row[0] == 3 || row[1] == 3 || row[2] == 3 || col[0] == 3 || col[1] == 3 || col[2] == 3 || diag == 3 || antidiag == 3; owin = row[0] == -3 || row[1] == -3 || row[2] == -3 || col[0] == -3 || col[1] == -3 || col[2] == -3 || diag == -3 || antidiag == -3; if ((xwin && turns == 0) || (owin && turns == 1)) return false; return (turns == 0 || turns == 1) && (!xwin || !owin); } };

 

Github 同步地址:

https://github.com/grandyang/leetcode/issues/794

 

相似题目:

Design Tic-Tac-Toe

 

参考资料:

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

https://leetcode.com/problems/valid-tic-tac-toe-state/discuss/117580/Straightforward-Java-solution-with-explaination

 

LeetCode All in One 题目讲解汇总(持续更新中...)

相关文章
相关标签/搜索