Skip to content

Commit f6f2eeb

Browse files
authored
Merge pull request halfrost#212 from gostool/leetcode0794
Leetcode0794
2 parents eaeed30 + fb35781 commit f6f2eeb

File tree

3 files changed

+214
-0
lines changed

3 files changed

+214
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package leetcode
2+
3+
func validTicTacToe(board []string) bool {
4+
cntX, cntO := 0, 0
5+
for i := range board {
6+
for j := range board[i] {
7+
if board[i][j] == 'X' {
8+
cntX++
9+
} else if board[i][j] == 'O' {
10+
cntO++
11+
}
12+
}
13+
}
14+
if cntX < cntO || cntX > cntO+1 {
15+
return false
16+
}
17+
if cntX == cntO {
18+
return process(board, 'X')
19+
}
20+
return process(board, 'O')
21+
}
22+
23+
func process(board []string, c byte) bool {
24+
//某一行是"ccc"
25+
if board[0] == string([]byte{c, c, c}) || board[1] == string([]byte{c, c, c}) || board[2] == string([]byte{c, c, c}) {
26+
return false
27+
}
28+
//某一列是"ccc"
29+
if (board[0][0] == c && board[1][0] == c && board[2][0] == c) ||
30+
(board[0][1] == c && board[1][1] == c && board[2][1] == c) ||
31+
(board[0][2] == c && board[1][2] == c && board[2][2] == c) {
32+
return false
33+
}
34+
//某一对角线是"ccc"
35+
if (board[0][0] == c && board[1][1] == c && board[2][2] == c) ||
36+
(board[0][2] == c && board[1][1] == c && board[2][0] == c) {
37+
return false
38+
}
39+
return true
40+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question794 struct {
9+
para794
10+
ans794
11+
}
12+
13+
// para 是参数
14+
type para794 struct {
15+
board []string
16+
}
17+
18+
// ans 是答案
19+
type ans794 struct {
20+
ans bool
21+
}
22+
23+
func Test_Problem794(t *testing.T) {
24+
25+
qs := []question794{
26+
27+
{
28+
para794{[]string{"O ", " ", " "}},
29+
ans794{false},
30+
},
31+
32+
{
33+
para794{[]string{"XOX", " X ", " "}},
34+
ans794{false},
35+
},
36+
37+
{
38+
para794{[]string{"XXX", " ", "OOO"}},
39+
ans794{false},
40+
},
41+
42+
{
43+
para794{[]string{"XOX", "O O", "XOX"}},
44+
ans794{true},
45+
},
46+
}
47+
48+
fmt.Printf("------------------------Leetcode Problem 794------------------------\n")
49+
50+
for _, q := range qs {
51+
_, p := q.ans794, q.para794
52+
fmt.Printf("【input】:%v 【output】:%v\n", p.board, validTicTacToe(p.board))
53+
}
54+
fmt.Printf("\n\n\n")
55+
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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+
![https://assets.leetcode.com/uploads/2021/05/15/tictactoe1-grid.jpg](https://assets.leetcode.com/uploads/2021/05/15/tictactoe1-grid.jpg)
21+
22+
Input: board = ["O "," "," "]
23+
Output: false
24+
Explanation: The first player always plays "X".
25+
26+
**Example 2**:
27+
28+
![https://assets.leetcode.com/uploads/2021/05/15/tictactoe2-grid.jpg](https://assets.leetcode.com/uploads/2021/05/15/tictactoe2-grid.jpg)
29+
30+
Input: board = ["XOX"," X "," "]
31+
Output: false
32+
Explanation: Players take turns making moves.
33+
34+
**Example 3**:
35+
36+
![https://assets.leetcode.com/uploads/2021/05/15/tictactoe3-grid.jpg](https://assets.leetcode.com/uploads/2021/05/15/tictactoe3-grid.jpg)
37+
38+
Input: board = ["XXX"," ","OOO"]
39+
Output: false
40+
41+
**Example 4**:
42+
43+
![https://assets.leetcode.com/uploads/2021/05/15/tictactoe4-grid.jpg](https://assets.leetcode.com/uploads/2021/05/15/tictactoe4-grid.jpg)
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

Comments
 (0)