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

 

這道題又是關於井字棋游戲的,之前也有一道類似的題 Design Tic-Tac-Toe,不過那道題是模擬游戲進行的,而這道題是讓驗證當前井字棋的游戲狀態是否正確。這題的例子給的比較好,cover 了很多種情況:

 

情況一:

0 _ _
_ _ _
_ _ _

 

這是不正確的狀態,因為先走的使用X,所以只出現一個O,是不對的。

 

情況二:

X O X
_ X _
_ _ _

 

這個也是不正確的,因為兩個 player 交替下棋,X最多只能比O多一個,這里多了兩個,肯定是不對的。

 

情況三:

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 題目講解匯總(持續更新中...)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM