On an 8 x 8
chessboard, there is exactly one white rook 'R'
and some number of white bishops 'B'
, black pawns 'p'
, and empty squares '.'
.
When the rook moves, it chooses one of four cardinal directions (north, east, south, or west), then moves in that direction until it chooses to stop, reaches the edge of the board, captures a black pawn, or is blocked by a white bishop. A rook is considered attacking a pawn if the rook can capture the pawn on the rook's turn. The number of available captures for the white rook is the number of pawns that the rook is attacking.
Return the number of available captures for the white rook.
Example 1:
Input: board = [[ ". ", ". ", ". ", ". ", ". ", ". ", ". ", ". "],[ ". ", ". ", ". ", "p ", ". ", ". ", ". ", ". "],[ ". ", ". ", ". ", "R ", ". ", ". ", ". ", "p "],[ ". ", ". ", ". ", ". ", ". ", ". ", ". ", ". "],[ ". ", ". ", ". ", ". ", ". ", ". ", ". ", ". "],[ ". ", ". ", ". ", "p ", ". ", ". ", ". ", ". "],[ ". ", ". ", ". ", ". ", ". ", ". ", ". ", ". "],[ ". ", ". ", ". ", ". ", ". ", ". ", ". ", ". "]] Output: 3 Explanation: In this example, the rook is attacking all the pawns.
Example 2:
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:
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'
board[i][j] == 'R'
program main
implicit none
character(len=1) :: board(8,8)
integer :: i, j, num_captures
! read input
do i = 1, 8
read(*,*) board(:,i)
end do
! solve problem
num_captures = count_rook_captures(board)
! print output
write(*,*) num_captures
contains
function count_rook_captures(board) result(num_captures)
implicit none
character(len=1), intent(in) :: board(8,8)
integer :: i, j, num_captures
num_captures = 0
do i = 1, 8
do j = 1, 8
if (board(i,j) == 'R') then
call count_rook_captures_helper(board, i, j, num_captures)
end if
end do
end do
end function count_rook_captures
subroutine count_rook_captures_helper(board, i, j, num_captures)
implicit none
character(len=1), intent(in) :: board(8,8)
integer, intent(in) :: i, j
integer, intent(out) :: num_captures
integer :: k, l
select case (board(i,j))
case ('R')
! north
k = i - 1
do while (k >= 1 .and. board(k,j) /= 'B')
if (board(k,j) == 'p') then
num_captures = num_captures + 1
end if
k = k - 1
end do
! east
l = j + 1
do while (l <= 8 .and. board(i,l) /= 'B')
if (board(i,l) == 'p') then
num_captures = num_captures + 1
end if
l = l + 1
end do
! south
k = i + 1
do while (k <= 8 .and. board(k,j) /= 'B')
if (board(k,j) == 'p') then
num_captures = num_captures + 1
end if
k = k + 1
end do
! west
l = j - 1
do while (l >= 1 .and. board(i,l) /= 'B')
if (board(i,l) == 'p') then
num_captures = num_captures + 1
end if
l = l - 1
end do
end select
end subroutine count_rook_captures_helper
end program main
At line 9 of file temp.f95 (unit = 5, file = 'stdin') Fortran runtime error: End of file Error termination. Backtrace: #0 0x78dfceae0960 in ??? #1 0x78dfceae14d9 in ??? #2 0x78dfced3517b in ??? #3 0x78dfced2e684 in ??? #4 0x78dfced2f2aa in ??? #5 0x78dfced34b7a in ??? #6 0x585291b9b51a in MAIN__ #7 0x585291b9b6a6 in main
!include(sort.f90)
program chess_rook
! Declare variables
integer, parameter :: N = 8
integer :: i, j, k, l, m, n, o, p
integer :: num_captures
character(len=1) :: board(N,N)
! Read input board
read(*,*) board
! Initialize num_captures
num_captures = 0
! Loop through rows
do i = 1, N
! Loop through columns
do j = 1, N
! Check if current cell is a white rook
if (board(i,j) == 'R') then
! Initialize variables
k = i
l = j
! Loop until rook reaches edge of board or captures a black pawn
do while (k > 0 .and. l > 0 .and. board(k,l) /= 'p')
! Check if current cell is a white bishop
if (board(k,l) == 'B') then
! Increment num_captures
num_captures = num_captures + 1
! Exit loop
exit
end if
! Decrement k and l
k = k - 1
l = l - 1
end do
! Loop until rook reaches edge of board or captures a black pawn
do while (k < N .and. l < N .and. board(k,l) /= 'p')
! Check if current cell is a white bishop
if (board(k,l) == 'B') then
! Increment num_captures
num_captures = num_captures + 1
! Exit loop
exit
end if
! Increment k and l
k = k + 1
l = l + 1
end do
! Loop until rook reaches edge of board or captures a black pawn
do while (k > 0 .and. l < N .and. board(k,l) /= 'p')
! Check if current cell is a white bishop
if (board(k,l) == 'B') then
! Increment num_captures
num_captures = num_captures + 1
! Exit loop
exit
end if
! Decrement k and l
k = k - 1
l = l + 1
end do
! Loop until rook reaches edge of board or captures a black pawn
do while (k < N .and. l > 0 .and. board(k,l) /= 'p')
! Check if current cell is a white bishop
if (board(k,l) == 'B') then
! Increment num_captures
num_captures = num_captures + 1
! Exit loop
exit
end if
! Increment k and l
k = k + 1
l = l - 1
end do
end do
end do
! Print output
write(*,*) num_captures
end program chess_rook
temp.f95:7:27: 7 | integer :: i, j, k, l, m, n, o, p | 1 Error: Symbol βnβ at (1) already has basic type of INTEGER temp.f95:110:3: 110 | end do | 1 Error: Expecting END IF statement at (1) temp.f95:112:3: 112 | end do | 1 Error: Expecting END IF statement at (1) temp.f95:117:3: 117 | end program chess_rook | 1 Error: Expecting END IF statement at (1) f951: Error: Unexpected end of file in βtemp.f95β
def regionsBySlashes(grid):
n = len(grid)
graph = [[0] * (n * 3) for _ in range(n * 3)]
for i in range(n):
for j in range(n):
if grid[i][j] == '/':
graph[i * 3][j * 3 + 2] = graph[i * 3 + 1][j * 3 + 1] = graph[i * 3 + 2][j * 3] = 1
if grid[i][j] == '\\':
graph[i * 3][j * 3] = graph[i * 3 + 1][j * 3 + 1] = graph[i * 3 + 2][j * 3 + 2] = 1
regions = 0
for i in range(n * 3):
for j in range(n * 3):
if not graph[i][j]:
regions += 1
dfs(graph, i, j)
return regions
def dfs(graph, i, j):
n = len(graph)
if i < 0 or j < 0 or i >= n or j >= n or graph[i][j]:
return
graph[i][j] = 1
dfs(graph, i - 1, j)
dfs(graph, i + 1, j)
dfs(graph, i, j - 1)
dfs(graph, i, j + 1)
The algorithm for solving this problem is as follows:
Create a graph representation of the n x n grid. The graph will have n * 3 rows and columns. The reason for this size is that we are going to divide the 1 x 1 grid cell with '/', '\', and ' ' into 9 smaller parts, making it easier to identify regions.
Loop through the grid and update the corresponding graph cells based on the '/' and '\' characters. For example, if we find '/' in grid[i][j], we update the graph at grid[i3][j3+2], grid[i3+1][j3+1], and grid[i3+2][j3] to 1.
Perform Depth-First Search (DFS) in the graph to count the number of regions.
Loop through the entire graph which represents the n x n grid. Whenever we find a cell with value 0, we increment the regions count and perform a DFS on that cell.
The DFS function will take graph, i, and j as input parameters. It will mark the cell as visited and call itself recursively for its neighboring cells.
The DFS function will stop when a cell is 1 or out of bounds.
Finally, we return the value of regions as the result. This will be the number of contiguous regions in the grid.
#include <vector>
#include <string>
int regionsBySlashes(std::vector<std::string>& grid) {
int n = grid.size();
std::vector<std::vector<int>> graph(n * 3, std::vector<int>(n * 3, 0));
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (grid[i][j] == '/') {
graph[i * 3][j * 3 + 2] = graph[i * 3 + 1][j * 3 + 1] = graph[i * 3 + 2][j * 3] = 1;
}
if (grid[i][j] == '\\') {
graph[i * 3][j * 3] = graph[i * 3 + 1][j * 3 + 1] = graph[i * 3 + 2][j * 3 + 2] = 1;
}
}
}
int regions = 0;
for (int i = 0; i < n * 3; ++i) {
for (int j = 0; j < n * 3; ++j) {
if (!graph[i][j]) {
regions++;
dfs(graph, i, j);
}
}
}
return regions;
}
void dfs(std::vector<std::vector<int>>& graph, int i, int j) {
int n = graph.size();
if (i < 0 || j < 0 || i >= n || j >= n || graph[i][j]) return;
graph[i][j] = 1;
dfs(graph, i - 1, j);
dfs(graph, i + 1, j);
dfs(graph, i, j - 1);
dfs(graph, i, j + 1);
}
The algorithm for solving this problem is as follows:
Create a graph representation of the n x n grid. The graph will have n * 3 rows and columns. The reason for this size is that we are going to divide the 1 x 1 grid cell with '/', '\', and ' ' into 9 smaller parts, making it easier to identify regions.
Loop through the grid and update the corresponding graph cells based on the '/' and '\' characters. For example, if we find '/' in grid[i][j], we update the graph at grid[i3][j3+2], grid[i3+1][j3+1], and grid[i3+2][j3] to 1.
Perform Depth-First Search (DFS) in the graph to count the number of regions.
Loop through the entire graph which represents the n x n grid. Whenever we find a cell with value 0, we increment the regions count and perform a DFS on that cell.
The DFS function will take graph, i, and j as input parameters. It will mark the cell as visited and call itself recursively for its neighboring cells.
The DFS function will stop when a cell is 1 or out of bounds.
Finally, we return the value of regions as the result. This will be the number of contiguous regions in the grid.