Movatterモバイル変換


[0]ホーム

URL:


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

Commitea8d5ff

Browse files
refactor 999
1 parent6cdd9af commitea8d5ff

File tree

1 file changed

+70
-142
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+70
-142
lines changed
Lines changed: 70 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -1,151 +1,79 @@
11
packagecom.fishercoder.solutions;
22

3-
/**
4-
* 999. Available Captures for Rook
5-
*
6-
* On an 8 x 8 chessboard, there is one white rook.
7-
* There also may be empty squares, white bishops, and black pawns.
8-
* These are given as characters 'R', '.', 'B', and 'p' respectively.
9-
* Uppercase characters represent white pieces, and lowercase characters represent black pieces.
10-
*
11-
* The rook moves as in the rules of Chess:
12-
* it chooses one of four cardinal directions (north, east, west, and south),
13-
* then moves in that direction until it chooses to stop, reaches the edge of the board,
14-
* or captures an opposite colored pawn by moving to the same square it occupies.
15-
* Also, rooks cannot move into the same square as other friendly bishops.
16-
*
17-
* Return the number of pawns the rook can capture in one move.
18-
*
19-
* Example 1:
20-
*
21-
* Input:[
22-
* 8 [".",".",".",".",".",".",".","."],
23-
* 7 [".",".",".","p",".",".",".","."],
24-
* 6 [".",".",".","R",".",".",".","p"],
25-
* 5 [".",".",".",".",".",".",".","."],
26-
* 4 [".",".",".",".",".",".",".","."],
27-
* 3 [".",".",".","p",".",".",".","."],
28-
* 2 [".",".",".",".",".",".",".","."],
29-
* 1 [".",".",".",".",".",".",".","."]]
30-
* a b c d e f g h
31-
*
32-
* Output: 3
33-
* Explanation:
34-
* In this example the rook is able to capture all the pawns.
35-
*
36-
* Example 2:
37-
*
38-
* Input:[
39-
* 8 [".",".",".",".",".",".",".","."],
40-
* 7 [".","p","p","p","p","p",".","."],
41-
* 6 [".","p","p","B","p","p",".","."],
42-
* 5 [".","p","B","R","B","p",".","."],
43-
* 4 [".","p","p","B","p","p",".","."],
44-
* 3 [".","p","p","p","p","p",".","."],
45-
* 2 [".",".",".",".",".",".",".","."],
46-
* 1 [".",".",".",".",".",".",".","."]]
47-
* a b c d e f g h
48-
*
49-
* Output: 0
50-
* Explanation:
51-
* Bishops are blocking the rook to capture any pawn.
52-
*
53-
* Example 3:
54-
*
55-
* Input: [
56-
* 8 [".",".",".",".",".",".",".","."],
57-
* 7 [".",".",".","p",".",".",".","."],
58-
* 6 [".",".",".","p",".",".",".","."],
59-
* 5 ["p","p",".","R",".","p","B","."],
60-
* 4 [".",".",".",".",".",".",".","."],
61-
* 3 [".",".",".","B",".",".",".","."],
62-
* 2 [".",".",".","p",".",".",".","."],
63-
* 1 [".",".",".",".",".",".",".","."]]
64-
* a b c d e f g h
65-
*
66-
* Output: 3
67-
* Explanation:
68-
* The rook can capture the pawns at positions b5, d6 and f5.
69-
*
70-
* Note:
71-
* board.length == board[i].length == 8
72-
* board[i][j] is either 'R', '.', 'B', or 'p'
73-
* There is exactly one cell with board[i][j] == 'R'
74-
*/
753
publicclass_999 {
76-
publicstaticclassSolution1 {
77-
int[]directions =newint[]{0,1,0, -1,0};
4+
publicstaticclassSolution1 {
5+
int[]directions =newint[]{0,1,0, -1,0};
786

79-
publicintnumRookCaptures(char[][]board) {
80-
intm =board.length;
81-
intn =board[0].length;
82-
introwR = -1;
83-
intcolR = -1;
84-
for (inti =0;i <m;i++) {
85-
for (intj =0;j <n;j++) {
86-
if (board[i][j] =='R') {
87-
rowR =i;
88-
colR =j;
89-
break;
90-
}
91-
}
92-
}
93-
intcount =0;
94-
for (inti =0;i <4;i++) {
95-
intneighborRow =rowR +directions[i];
96-
intneighborCol =colR +directions[i +1];
97-
if (neighborRow >=0 &&neighborRow <m
98-
&&neighborCol >=0 &&neighborCol <n
99-
&&board[neighborRow][neighborCol] !='B') {
100-
if (directions[i] ==0 &&directions[i +1] ==1) {
101-
while (neighborCol <n) {
102-
if (board[neighborRow][neighborCol] =='p') {
103-
count++;
104-
break;
105-
}elseif (board[neighborRow][neighborCol] =='B') {
106-
break;
107-
}else {
108-
neighborCol++;
109-
}
110-
}
111-
}elseif (directions[i] ==1 &&directions[i +1] ==0) {
112-
while (neighborRow <m) {
113-
if (board[neighborRow][neighborCol] =='p') {
114-
count++;
115-
break;
116-
}elseif (board[neighborRow][neighborCol] =='B') {
117-
break;
118-
}else {
119-
neighborRow++;
120-
}
121-
}
122-
}elseif (directions[i] ==0 &&directions[i +1] == -1) {
123-
while (neighborCol >=0) {
124-
if (board[neighborRow][neighborCol] =='p') {
125-
count++;
126-
break;
127-
}elseif (board[neighborRow][neighborCol] =='B') {
128-
break;
129-
}else {
130-
neighborCol--;
131-
}
7+
publicintnumRookCaptures(char[][]board) {
8+
intm =board.length;
9+
intn =board[0].length;
10+
introwR = -1;
11+
intcolR = -1;
12+
for (inti =0;i <m;i++) {
13+
for (intj =0;j <n;j++) {
14+
if (board[i][j] =='R') {
15+
rowR =i;
16+
colR =j;
17+
break;
18+
}
19+
}
13220
}
133-
}else {
134-
while (neighborRow >=0) {
135-
if (board[neighborRow][neighborCol] =='p') {
136-
count++;
137-
break;
138-
}elseif (board[neighborRow][neighborCol] =='B') {
139-
break;
140-
}else {
141-
neighborRow--;
142-
}
21+
intcount =0;
22+
for (inti =0;i <4;i++) {
23+
intneighborRow =rowR +directions[i];
24+
intneighborCol =colR +directions[i +1];
25+
if (neighborRow >=0 &&neighborRow <m
26+
&&neighborCol >=0 &&neighborCol <n
27+
&&board[neighborRow][neighborCol] !='B') {
28+
if (directions[i] ==0 &&directions[i +1] ==1) {
29+
while (neighborCol <n) {
30+
if (board[neighborRow][neighborCol] =='p') {
31+
count++;
32+
break;
33+
}elseif (board[neighborRow][neighborCol] =='B') {
34+
break;
35+
}else {
36+
neighborCol++;
37+
}
38+
}
39+
}elseif (directions[i] ==1 &&directions[i +1] ==0) {
40+
while (neighborRow <m) {
41+
if (board[neighborRow][neighborCol] =='p') {
42+
count++;
43+
break;
44+
}elseif (board[neighborRow][neighborCol] =='B') {
45+
break;
46+
}else {
47+
neighborRow++;
48+
}
49+
}
50+
}elseif (directions[i] ==0 &&directions[i +1] == -1) {
51+
while (neighborCol >=0) {
52+
if (board[neighborRow][neighborCol] =='p') {
53+
count++;
54+
break;
55+
}elseif (board[neighborRow][neighborCol] =='B') {
56+
break;
57+
}else {
58+
neighborCol--;
59+
}
60+
}
61+
}else {
62+
while (neighborRow >=0) {
63+
if (board[neighborRow][neighborCol] =='p') {
64+
count++;
65+
break;
66+
}elseif (board[neighborRow][neighborCol] =='B') {
67+
break;
68+
}else {
69+
neighborRow--;
70+
}
71+
}
72+
}
73+
}
14374
}
144-
}
145-
}
146-
}
14775

148-
returncount;
76+
returncount;
77+
}
14978
}
150-
}
15179
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp