Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4485d63

Browse files
author
openset
committed
Add: Available Captures for Rook
1 parent d858ac7 commit 4485d63

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package available_captures_for_rook
2+
3+
func numRookCaptures(board [][]byte) int {
4+
ans, rx, ry, left, up := 0, -1, -1, [8]int{}, [8]int{}
5+
for i, row := range board {
6+
for j, v := range row {
7+
if rx == -1 {
8+
if v == 'p' {
9+
left[i], up[j] = 1, 1
10+
} else if v == 'B' {
11+
left[i], up[j] = 0, 0
12+
} else if v == 'R' {
13+
ans, rx, ry = left[i]+up[j], i, j
14+
}
15+
} else if i == rx {
16+
if v == 'p' {
17+
ans++
18+
}
19+
if v != '.' {
20+
break
21+
}
22+
} else if j == ry {
23+
if v == 'p' {
24+
ans++
25+
}
26+
if v != '.' {
27+
return ans
28+
}
29+
}
30+
}
31+
}
32+
return ans
33+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package available_captures_for_rook
2+
3+
import "testing"
4+
5+
type caseType struct {
6+
input [][]byte
7+
expected int
8+
}
9+
10+
func TestNumRookCaptures(t *testing.T) {
11+
tests := [...]caseType{
12+
{
13+
input: [][]byte{
14+
{'.', '.', '.', '.', '.', '.', '.', '.'},
15+
{'.', '.', '.', 'p', '.', '.', '.', '.'},
16+
{'.', '.', '.', 'R', '.', '.', '.', 'p'},
17+
{'.', '.', '.', '.', '.', '.', '.', '.'},
18+
{'.', '.', '.', '.', '.', '.', '.', '.'},
19+
{'.', '.', '.', 'p', '.', '.', '.', '.'},
20+
{'.', '.', '.', '.', '.', '.', '.', '.'},
21+
{'.', '.', '.', '.', '.', '.', '.', '.'},
22+
},
23+
expected: 3,
24+
},
25+
{
26+
input: [][]byte{
27+
{'.', '.', '.', '.', '.', '.', '.', '.'},
28+
{'.', 'p', 'p', 'p', 'p', 'p', '.', '.'},
29+
{'.', 'p', 'p', 'B', 'p', 'p', '.', '.'},
30+
{'.', 'p', 'B', 'R', 'B', 'p', '.', '.'},
31+
{'.', 'p', 'p', 'B', 'p', 'p', '.', '.'},
32+
{'.', 'p', 'p', 'p', 'p', 'p', '.', '.'},
33+
{'.', '.', '.', '.', '.', '.', '.', '.'},
34+
{'.', '.', '.', '.', '.', '.', '.', '.'},
35+
},
36+
expected: 0,
37+
},
38+
{
39+
input: [][]byte{
40+
{'.', '.', '.', '.', '.', '.', '.', '.'},
41+
{'.', '.', '.', 'p', '.', '.', '.', '.'},
42+
{'.', '.', '.', 'p', '.', '.', '.', '.'},
43+
{'p', 'p', '.', 'R', '.', 'p', 'B', '.'},
44+
{'.', '.', '.', '.', '.', '.', '.', '.'},
45+
{'.', '.', '.', 'B', '.', '.', '.', '.'},
46+
{'.', '.', '.', 'p', '.', '.', '.', '.'},
47+
{'.', '.', '.', '.', '.', '.', '.', '.'},
48+
},
49+
expected: 3,
50+
},
51+
}
52+
for _, tc := range tests {
53+
output := numRookCaptures(tc.input)
54+
if output != tc.expected {
55+
t.Fatalf("input: %v, output: %v, expected: %v", tc.input, output, tc.expected)
56+
}
57+
}
58+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /