1. 程式人生 > >[LeetCode] Valid Tic-Tac-Toe State 驗證井字棋狀態

[LeetCode] 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);
    }
};

類似題目:

參考資料: