1041. Available Captures for Rook
My accepted C++ solution to LeetCode problem 1041, Available Captures for Rook, running in 0ms.
- Difficulty: Easy
- C++
- Runtime 0ms
- Memory 9.2MB
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
C++
Accepted on LeetCode — runtime 0ms, memory 9.2MB, accepted 2025-12-27.
class Solution {
public:
int numRookCaptures(vector<vector<char>>& board) {
int rx, ry;
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if (board[i][j] == 'R') { rx = i; ry = j; }
}
}
int count = 0;
int dirs[4][2] = {{0,1},{0,-1},{1,0},{-1,0}};
for (auto& d : dirs) {
int x = rx + d[0], y = ry + d[1];
while (x >= 0 && x < 8 && y >= 0 && y < 8) {
if (board[x][y] == 'B') break;
if (board[x][y] == 'p') { count++; break; }
x += d[0]; y += d[1];
}
}
return count;
}
};