# 【LeetCode每日一题】车的可用捕获量

[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".",“p”,".",".",".","."],
[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]]

[[".",".",".",".",".",".",".","."],[".",“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”,".","."],
[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]]

[[".",".",".",".",".",".",".","."],[".",".",".",“p”,".",".",".","."],[".",".",".",“p”,".",".",".","."],
[“p”,“p”,".",“R”,".",“p”,“B”,"."],[".",".",".",".",".",".",".","."],[".",".",".",“B”,".",".",".","."],
[".",".",".",“p”,".",".",".","."],[".",".",".",".",".",".",".","."]]

board.length == board[i].length == 8
board[i][j] 可以是 'R'，'.'，'B' 或 'p'



#### C++1 循环遍历

1、先找白车R的位置，找到之后用tx,ty记下。
2、上下左右四个方向用两个数组保存，然后从白车的位置分别遍历四个方向上有无黑卒p，找到则将最后结果res加一，跳出此方向，否则一直走到头（当第一次遇到黑卒且之前没有白象时才算做计数）；

class Solution {
public:
int numRookCaptures(vector<vector<char>>& board) {
int h = board.size();
int w = board[0].size();
int tx,ty;
for(int i=0;i<h;i++){
for(int j=0;j<w;j++){
if(board[i][j] == 'R'){
tx = i;
ty = j;
break;
}
}
}
int res = 0;
int dx[] = {-1,1,0,0};
int dy[] = {0,0,1,-1};
for(int k=0;k<4;k++){
int flag = 0; // 0表示白车可以往前走，找到白象时设为1
int x = tx, y = ty;
x = x+dx[k];
y = y+dy[k];
while(x>=0&&x<h&&y>=0&&y<w){
if(flag == 1){
break;
}
if(board[x][y] == 'p'&& flag == 0){
res++;
break;
}else if(board[x][y] == 'B'){
flag = 1;
}
x = x+dx[k];
y = y+dy[k];
}
}
return res;
}
};


#### 复杂度分析

©️2019 CSDN 皮肤主题: 黑客帝国 设计师: 上身试试