https://leetcode.com/problems/available-captures-for-rook/
https://leetcode.com/problems/available-captures-for-rook/discuss/242924/C%2B%2BJava-search-and-capture
On an 8 x 8 chessboard, there is one white rook. There also may be empty squares, white bishops, and black pawns. These are given as characters 'R', '.', 'B', and 'p' respectively. Uppercase characters represent white pieces, and lowercase characters represent black pieces.
The rook moves as in the rules of Chess: it chooses one of four cardinal directions (north, east, west, and south), then moves in that direction until it chooses to stop, reaches the edge of the board, or captures an opposite colored pawn by moving to the same square it occupies. Also, rooks cannot move into the same square as other friendly bishops.
Return the number of pawns the rook can capture in one move.
https://leetcode.com/problems/available-captures-for-rook/discuss/242924/C%2B%2BJava-search-and-capture
Search for the rock. From the rock, trace empty spaces in four directions. Return 1 if we hit a pawn, zero otherwise.
Count and return captured pawns.
int cap(char[][] b, int x, int y, int dx, int dy) {
while (x >= 0 && x < b.length && y >= 0 && y < b[x].length && b[x][y] != 'B') {
if (b[x][y] == 'p') return 1;
x += dx; y += dy;
}
return 0;
}
public int numRookCaptures(char[][] b) {
for (int i = 0; i < b.length; ++i)
for (int j = 0; j < b[i].length; ++j)
if (b[i][j] == 'R') return cap(b,i,j,0,1)+cap(b,i,j,0,-1)+cap(b,i,j,1,0)+cap(b,i,j,-1,0);
return 0;
}
https://leetcode.com/problems/available-captures-for-rook/discuss/243007/Easy-Java-Solution public int numRookCaptures(char[][] board) {
int[] point = new int[2];
for(int i = 0 ; i < 8 ; i++) {
for(int j = 0 ; j < 8 ; j++) {
if(board[i][j] == 'R') {
point[0] = i;
point[1] = j;
break;
}
}
}
int count = 0;
int[][] dirs = {{1,0},{-1,0},{0,1},{0,-1}};
for(int[] dir : dirs) {
int x = point[0] + dir[0];
int y = point[1] + dir[1];
while(x >= 0 && y >= 0 && x < 8 && y < 8 && board[x][y] == '.') {
x += dir[0];
y += dir[1];
}
if(x < 0 || y < 0 || x >= 8 || y >= 8) continue;
if(board[x][y] == 'p') count++;
}
return count;
}