找到车然后朝四个方向延申,看看第一个棋是不是友军就行了
class Solution {
public:
int numRookCaptures(vector<vector<char>>& board) {
int ans=0;
for(int i=0;i<8;i++)
for(int j=0;j<8;j++)
if(board[i][j]=='R')
{
int x1,x2,y1,y2;
x1=x2=i,y1=y2=j;
while(x1>0)
if(board[--x1][j]!='.')
{
if(board[x1][j]=='p') ans++;
break;
}
while(x2<7)
if(board[++x2][j]!='.')
{
if(board[x2][j]=='p') ans++;
break;
}
while(y1>0)
if(board[i][--y1]!='.')
{
if(board[i][y1]=='p') ans++;
break;
}
while(y2<7)
if(board[i][++y2]!='.')
{
if(board[i][y2]=='p') ans++;
break;
}
break;
}
return ans;
}
};