Skip to content

Commit ea8d5ff

Browse files
refactor 999
1 parent 6cdd9af commit ea8d5ff

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
package com.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
public class _999 {
76-
public static class Solution1 {
77-
int[] directions = new int[] {0, 1, 0, -1, 0};
4+
public static class Solution1 {
5+
int[] directions = new int[]{0, 1, 0, -1, 0};
786

79-
public int numRookCaptures(char[][] board) {
80-
int m = board.length;
81-
int n = board[0].length;
82-
int rowR = -1;
83-
int colR = -1;
84-
for (int i = 0; i < m; i++) {
85-
for (int j = 0; j < n; j++) {
86-
if (board[i][j] == 'R') {
87-
rowR = i;
88-
colR = j;
89-
break;
90-
}
91-
}
92-
}
93-
int count = 0;
94-
for (int i = 0; i < 4; i++) {
95-
int neighborRow = rowR + directions[i];
96-
int neighborCol = 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-
} else if (board[neighborRow][neighborCol] == 'B') {
106-
break;
107-
} else {
108-
neighborCol++;
109-
}
110-
}
111-
} else if (directions[i] == 1 && directions[i + 1] == 0) {
112-
while (neighborRow < m) {
113-
if (board[neighborRow][neighborCol] == 'p') {
114-
count++;
115-
break;
116-
} else if (board[neighborRow][neighborCol] == 'B') {
117-
break;
118-
} else {
119-
neighborRow++;
120-
}
121-
}
122-
} else if (directions[i] == 0 && directions[i + 1] == -1) {
123-
while (neighborCol >= 0) {
124-
if (board[neighborRow][neighborCol] == 'p') {
125-
count++;
126-
break;
127-
} else if (board[neighborRow][neighborCol] == 'B') {
128-
break;
129-
} else {
130-
neighborCol--;
131-
}
7+
public int numRookCaptures(char[][] board) {
8+
int m = board.length;
9+
int n = board[0].length;
10+
int rowR = -1;
11+
int colR = -1;
12+
for (int i = 0; i < m; i++) {
13+
for (int j = 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-
} else if (board[neighborRow][neighborCol] == 'B') {
139-
break;
140-
} else {
141-
neighborRow--;
142-
}
21+
int count = 0;
22+
for (int i = 0; i < 4; i++) {
23+
int neighborRow = rowR + directions[i];
24+
int neighborCol = 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+
} else if (board[neighborRow][neighborCol] == 'B') {
34+
break;
35+
} else {
36+
neighborCol++;
37+
}
38+
}
39+
} else if (directions[i] == 1 && directions[i + 1] == 0) {
40+
while (neighborRow < m) {
41+
if (board[neighborRow][neighborCol] == 'p') {
42+
count++;
43+
break;
44+
} else if (board[neighborRow][neighborCol] == 'B') {
45+
break;
46+
} else {
47+
neighborRow++;
48+
}
49+
}
50+
} else if (directions[i] == 0 && directions[i + 1] == -1) {
51+
while (neighborCol >= 0) {
52+
if (board[neighborRow][neighborCol] == 'p') {
53+
count++;
54+
break;
55+
} else if (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+
} else if (board[neighborRow][neighborCol] == 'B') {
67+
break;
68+
} else {
69+
neighborRow--;
70+
}
71+
}
72+
}
73+
}
14374
}
144-
}
145-
}
146-
}
14775

148-
return count;
76+
return count;
77+
}
14978
}
150-
}
15179
}

0 commit comments

Comments
 (0)