车的可用捕获量
Available Captures for Rook
解
题目本身很简单,但是要注意题目描述的理解“返回车能够在一次移动中捕获到的卒的数量”
直接模拟就可以。
class Solution {
public:
int numRookCaptures(vector<vector<char>>& board) {
int count = 0, x = 0, y = 0;
int dx[4] = { 0, 1, 0, -1 };
int dy[4] = { 1, 0, -1, 0 };
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if (board[i][j] == 'R') {
x = i;
y = j;
break;
}
}
}
for (int i = 0; i < 4; i++) {
for (int j = 0;; j++) {
int tx = x + j * dx[i];
int ty = y + j * dy[i];
if (tx < 0 || tx >= 8 || ty < 0 || ty >= 8 || board[tx][ty] == 'B') {
break;
}
if (board[tx][ty] == 'p') {
count++;
break;
}
}
}
return count;
}
};