-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path999-AvailableCapturesForRook.go
More file actions
149 lines (138 loc) · 6.21 KB
/
999-AvailableCapturesForRook.go
File metadata and controls
149 lines (138 loc) · 6.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package main
// 999. Available Captures for Rook
// You are given an 8 x 8 matrix representing a chessboard.
// There is exactly one white rook represented by 'R', some number of white bishops 'B', and some number of black pawns 'p'.
// Empty squares are represented by '.'.
// A rook can move any number of squares horizontally or vertically (up, down, left, right)
// until it reaches another piece or the edge of the board.
// A rook is attacking a pawn if it can move to the pawn's square in one move.
// Note: A rook cannot move through other pieces, such as bishops or pawns.
// This means a rook cannot attack a pawn if there is another piece blocking the path.
// Return the number of pawns the white rook is attacking.
// Example 1:
// <img src="https://assets.leetcode.com/uploads/2019/02/20/1253_example_1_improved.PNG" />
// Input: board = [[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".","R",".",".",".","p"],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]]
// Output: 3
// Explanation:
// In this example, the rook is attacking all the pawns.
// Example 2:
// <img src="https://assets.leetcode.com/uploads/2019/02/19/1253_example_2_improved.PNG" />
// Input: board = [[".",".",".",".",".",".","."],[".","p","p","p","p","p",".","."],[".","p","p","B","p","p",".","."],[".","p","B","R","B","p",".","."],[".","p","p","B","p","p",".","."],[".","p","p","p","p","p",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]]
// Output: 0
// Explanation:
// The bishops are blocking the rook from attacking any of the pawns.
// Example 3:
// <img src="https://assets.leetcode.com/uploads/2019/02/20/1253_example_3_improved.PNG" />
// Input: board = [[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".","p",".",".",".","."],["p","p",".","R",".","p","B","."],[".",".",".",".",".",".",".","."],[".",".",".","B",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".",".",".",".",".","."]]
// Output: 3
// Explanation:
// The rook is attacking the pawns at positions b5, d6, and f5.
// Constraints:
// board.length == 8
// board[i].length == 8
// board[i][j] is either 'R', '.', 'B', or 'p'
// There is exactly one cell with board[i][j] == 'R'
import "fmt"
func numRookCaptures(board [][]byte) int {
res, y, x := 0, -1, -1
for i := 0; i < 8; i++ { // 找到车的位置
for j := 0; j < 8; j++ {
if board[i][j] == 'R' {
y = i
x = j
break
}
}
if y != -1 { break }
}
i, j := y + 1, x // 向下
for i < 8 {
if board[i][j] == 'B' { break } // 遇到象停止
if board[i][j] == 'p' {
res++
break
}
i++
}
i, j = y, x - 1 // 向左
for j >= 0 {
if board[i][j] == 'B' { break } // 遇到象停止
if board[i][j] == 'p' {
res++
break
}
j--
}
i, j = y - 1, x // 向上
for i >= 0 {
if board[i][j] == 'B' { break } // 遇到象停止
if board[i][j] == 'p' {
res++
break
}
i--
}
i, j = y, x + 1 // 向右
for j < 8 {
if board[i][j] == 'B' { break } // 遇到象停止
if board[i][j] == 'p' {
res++
break
}
j++
}
return res
}
func main() {
// Example 1:
// <img src="https://assets.leetcode.com/uploads/2019/02/20/1253_example_1_improved.PNG" />
// Input: board = [[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".","R",".",".",".","p"],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]]
// Output: 3
// Explanation:
// In this example, the rook is attacking all the pawns.
board1 := [][]byte{
{'.','.','.','.','.','.','.','.'},
{'.','.','.','p','.','.','.','.'},
{'.','.','.','R','.','.','.','p'},
{'.','.','.','.','.','.','.','.'},
{'.','.','.','.','.','.','.','.'},
{'.','.','.','p','.','.','.','.'},
{'.','.','.','.','.','.','.','.'},
{'.','.','.','.','.','.','.','.'},
}
fmt.Println(numRookCaptures(board1)) // 3
// Example 2:
// <img src="https://assets.leetcode.com/uploads/2019/02/19/1253_example_2_improved.PNG" />
// Input: board = [[".",".",".",".",".",".","."],[".","p","p","p","p","p",".","."],[".","p","p","B","p","p",".","."],[".","p","B","R","B","p",".","."],[".","p","p","B","p","p",".","."],[".","p","p","p","p","p",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]]
// Output: 0
// Explanation:
// The bishops are blocking the rook from attacking any of the pawns.
board2 := [][]byte{
{'.','.','.','.','.','.','.','.'},
{'.','p','p','p','p','p','.','.'},
{'.','p','p','B','p','p','.','.'},
{'.','p','B','R','B','p','.','.'},
{'.','p','p','B','p','p','.','.'},
{'.','p','p','p','p','p','.','.'},
{'.','.','.','.','.','.','.','.'},
{'.','.','.','.','.','.','.','.'},
}
fmt.Println(numRookCaptures(board2)) // 0
// Example 3:
// <img src="https://assets.leetcode.com/uploads/2019/02/20/1253_example_3_improved.PNG" />
// Input: board = [[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".","p",".",".",".","."],["p","p",".","R",".","p","B","."],[".",".",".",".",".",".",".","."],[".",".",".","B",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".",".",".",".",".","."]]
// Output: 3
// Explanation:
// The rook is attacking the pawns at positions b5, d6, and f5.
board3 := [][]byte{
{'.','.','.','.','.','.','.','.'},
{'.','.','.','p','.','.','.','.'},
{'.','.','.','p','.','.','.','.'},
{'p','p','.','R','.','p','B','.'},
{'.','.','.','.','.','.','.','.'},
{'.','.','.','B','.','.','.','.'},
{'.','.','.','p','.','.','.','.'},
{'.','.','.','.','.','.','.','.'},
}
fmt.Println(numRookCaptures(board3)) // 3
}