力扣第999题 车的可用捕获量

力扣第999题 车的可用捕获量

class Solution {
    public:
    int numRookCaptures(vector<vector<char>>& board) {
        int num = 0;
        for (int i = 0; i < 8; i++)
        {
            for (int j = 0; j < 8; j++)
            {
                if (board[i][j] == 'R')
                {
                    int temp = 1;
                    while(i - temp >=0 && board[i - temp][j] != 'B')
                    {
                        if (board[i - temp][j] == 'p'){num++;break;}
                        temp++;
                    }
                    temp = 1;
                    while (i + temp < 8 && board[i + temp][j] != 'B')
                    {
                        if (board[i + temp][j] == 'p'){num++;break;}
                        temp++;
                    }
                    temp = 1;
                    while (j - temp >= 0 && board[i][j - temp] != 'B')
                    {
                        if (board[i][j - temp] == 'p'){num++;break;}
                        temp++;
                    }
                    temp = 1;
                    while (j + temp < 8 && board[i][j + temp] != 'B')
                    {
                        if (board[i][j + temp] == 'p'){num++;break;}
                        temp++;
                    }
                    return num;
                }
            }
        }
        return num;
    }
};

猜你喜欢

转载自www.cnblogs.com/woodjay/p/12577841.html