1. 程式人生 > 其它 >[LeetCode] 794. Valid Tic-Tac-Toe State

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

Given a Tic-Tac-Toe board as a string arrayboard, returntrueif and only if it is possible to reach this board position during the course of a valid tic-tac-toe game.

The board is a3 x 3array that 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 three 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

Constraints:

  • board.length == 3
  • board[i].length == 3
  • board[i][j]is either'X','O', or' '.

有效的井字遊戲。

給你一個字串陣列 board 表示井字遊戲的棋盤。當且僅當在井字遊戲過程中,棋盤有可能達到 board 所顯示的狀態時,才返回 true 。

井字遊戲的棋盤是一個 3 x 3 陣列,由字元 ' ','X' 和 'O' 組成。字元 ' ' 代表一個空位。

以下是井字遊戲的規則:

玩家輪流將字元放入空位(' ')中。
玩家 1 總是放字元 'X' ,而玩家 2 總是放字元 'O' 。
'X' 和 'O' 只允許放置在空位中,不允許對已放有字元的位置進行填充。
當有 3 個相同(且非空)的字元填充任何行、列或對角線時,遊戲結束。
當所有位置非空時,也算為遊戲結束。
如果遊戲結束,玩家不允許再放置字元。

來源:力扣(LeetCode)
連結:https://leetcode-cn.com/problems/valid-tic-tac-toe-state
著作權歸領釦網路所有。商業轉載請聯絡官方授權,非商業轉載請註明出處。

這道題的題意寫的不明確,我這裡做一點說明。給的是一個3x3的棋盤,請你判斷目前棋盤上的形勢是否有可能達到。所謂達到的意思是如果兩個玩家之間有一個已經勝出了,則無法達到當前 input 給的這個狀態;如果在正常下棋的過程中能達到這種狀態,則返回 true。對於井字棋的規則,則比較簡單了。兩個玩家 X 和 O,只要有一個玩家能在何行、列或對角線時,遊戲結束。

這是一道實現題,我們判斷的規則是

  • 因為 X 先走所以 O 的數量是不可能大於 X 的
  • 因為每個人輪流走所以兩者差距不會大於 1
  • 如果x獲勝但是X的數量小於O,則不成立
  • 如果O獲勝但是O的數量小於X,則不成立
  • 兩者不可能同時獲勝

照著這幾個要求,程式碼就比較好寫了。

時間O(1) - 只需要判斷九個空格即可

空間O(1)

Java實現

 1 class Solution {
 2     public boolean validTicTacToe(String[] board) {
 3         char[][] res = new char[3][3];
 4         int x = 0;
 5         int o = 0;
 6         for (int i = 0; i < 3; i++) {
 7             for (int j = 0; j < 3; j++) {
 8                 char c = board[i].charAt(j);
 9                 if (c == 'X') {
10                     x++;
11                 } else if (c == 'O') {
12                     o++;
13                 }
14                 res[i][j] = c;
15             }
16         }
17         boolean a = check(res, 'X');
18         boolean b = check(res, 'O');
19         // System.out.println("x is " + x);
20         // System.out.println("o is " + o);
21         // 因為X先走所以O的數量是不可能大於X的
22         // 且因為每個人輪流走所以兩者差距不會大於1
23         if (o > x || x - o > 1) {
24             return false;
25         }
26         // 如果x獲勝但是X的數量小於O,則不成立
27         if (a && x <= o) {
28             return false;
29         }
30         // 如果O獲勝但是O的數量小於X,則不成立
31         if (b && o != x) {
32             return false;
33         }
34         // 兩者不可能同時獲勝
35         if (a && b) {
36             return false;
37         }
38         return true;
39     }
40 
41     private boolean check(char[][] board, char c) {
42         for (int i = 0; i < 3; i++) {
43             if (board[i][0] == c && board[i][1] == c && board[i][2] == c) {
44                 return true;
45             }
46         }
47         for (int i = 0; i < 3; i++) {
48             if (board[0][i] == c && board[1][i] == c && board[2][i] == c) {
49                 return true;
50             }
51         }
52         if (board[0][0] == c && board[1][1] == c && board[2][2] == c) {
53             return true;
54         }
55         if (board[2][0] == c && board[1][1] == c && board[0][2] == c) {
56             return true;
57         }
58         return false;
59     }
60 }

LeetCode 題目總結