|
| 1 | +# [794. Valid Tic-Tac-Toe State](https://leetcode-cn.com/problems/valid-tic-tac-toe-state/) |
| 2 | + |
| 3 | +## 题目 |
| 4 | + |
| 5 | +Given a Tic-Tac-Toe board 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. |
| 6 | + |
| 7 | +The board is a 3 x 3 array that consists of characters ' ', 'X', and 'O'. The ' ' character represents an empty square. |
| 8 | + |
| 9 | +Here are the rules of Tic-Tac-Toe: |
| 10 | + |
| 11 | +- Players take turns placing characters into empty squares ' '. |
| 12 | +- The first player always places 'X' characters, while the second player always places 'O' characters. |
| 13 | +- 'X' and 'O' characters are always placed into empty squares, never filled ones. |
| 14 | +- The game ends when there are three of the same (non-empty) character filling any row, column, or diagonal. |
| 15 | +- The game also ends if all squares are non-empty. |
| 16 | +- No more moves can be played if the game is over. |
| 17 | + |
| 18 | +**Example 1**: |
| 19 | + |
| 20 | + |
| 21 | + |
| 22 | + Input: board = ["O "," "," "] |
| 23 | + Output: false |
| 24 | + Explanation: The first player always plays "X". |
| 25 | + |
| 26 | +**Example 2**: |
| 27 | + |
| 28 | + |
| 29 | + |
| 30 | + Input: board = ["XOX"," X "," "] |
| 31 | + Output: false |
| 32 | + Explanation: Players take turns making moves. |
| 33 | + |
| 34 | +**Example 3**: |
| 35 | + |
| 36 | + |
| 37 | + |
| 38 | + Input: board = ["XXX"," ","OOO"] |
| 39 | + Output: false |
| 40 | + |
| 41 | +**Example 4**: |
| 42 | + |
| 43 | + |
| 44 | + |
| 45 | + Input: board = ["XOX","O O","XOX"] |
| 46 | + Output: true |
| 47 | + |
| 48 | +**Constraints:** |
| 49 | + |
| 50 | +- board.length == 3 |
| 51 | +- board[i].length == 3 |
| 52 | +- board[i][j] is either 'X', 'O', or ' '. |
| 53 | + |
| 54 | +## 题目大意 |
| 55 | + |
| 56 | +给你一个字符串数组 board 表示井字游戏的棋盘。当且仅当在井字游戏过程中,棋盘有可能达到 board 所显示的状态时,才返回 true 。 |
| 57 | + |
| 58 | +井字游戏的棋盘是一个 3 x 3 数组,由字符 ' ','X' 和 'O' 组成。字符 ' ' 代表一个空位。 |
| 59 | + |
| 60 | +以下是井字游戏的规则: |
| 61 | + |
| 62 | +- 玩家轮流将字符放入空位(' ')中。 |
| 63 | +- 玩家 1 总是放字符 'X' ,而玩家 2 总是放字符 'O' 。 |
| 64 | +- 'X' 和 'O' 只允许放置在空位中,不允许对已放有字符的位置进行填充。 |
| 65 | +- 当有 3 个相同(且非空)的字符填充任何行、列或对角线时,游戏结束。 |
| 66 | +- 当所有位置非空时,也算为游戏结束。 |
| 67 | +- 如果游戏结束,玩家不允许再放置字符。 |
| 68 | + |
| 69 | +## 解题思路 |
| 70 | + |
| 71 | +分类模拟: |
| 72 | +- 根据题意棋盘在任意时候,要么 X 的数量比 O 的数量多 1,要么两者相等 |
| 73 | +- X 的数量等于 O 的数量时,任何行、列或对角线都不会出现 3 个相同的 X |
| 74 | +- X 的数量比 O 的数量多 1时,任何行、列或对角线都不会出现 3 个相同的 O |
| 75 | + |
| 76 | +## 代码 |
| 77 | + |
| 78 | +```go |
| 79 | +package leetcode |
| 80 | + |
| 81 | +func validTicTacToe(board []string) bool { |
| 82 | + cntX, cntO := 0, 0 |
| 83 | + for i := range board { |
| 84 | + for j := range board[i] { |
| 85 | + if board[i][j] == 'X' { |
| 86 | + cntX++ |
| 87 | + } else if board[i][j] == 'O' { |
| 88 | + cntO++ |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + if cntX < cntO || cntX > cntO+1 { |
| 93 | + return false |
| 94 | + } |
| 95 | + if cntX == cntO { |
| 96 | + return process(board, 'X') |
| 97 | + } |
| 98 | + return process(board, 'O') |
| 99 | +} |
| 100 | + |
| 101 | +func process(board []string, c byte) bool { |
| 102 | + //某一行是"ccc" |
| 103 | + if board[0] == string([]byte{c, c, c}) || board[1] == string([]byte{c, c, c}) || board[2] == string([]byte{c, c, c}) { |
| 104 | + return false |
| 105 | + } |
| 106 | + //某一列是"ccc" |
| 107 | + if (board[0][0] == c && board[1][0] == c && board[2][0] == c) || |
| 108 | + (board[0][1] == c && board[1][1] == c && board[2][1] == c) || |
| 109 | + (board[0][2] == c && board[1][2] == c && board[2][2] == c) { |
| 110 | + return false |
| 111 | + } |
| 112 | + //某一对角线是"ccc" |
| 113 | + if (board[0][0] == c && board[1][1] == c && board[2][2] == c) || |
| 114 | + (board[0][2] == c && board[1][1] == c && board[2][0] == c) { |
| 115 | + return false |
| 116 | + } |
| 117 | + return true |
| 118 | +} |
| 119 | +``` |
0 commit comments