Available Captures for Rook

On an 8 x 8 chessboard, there is exactly one white rook 'R' and some number of white bishops 'B', black pawns 'p', and empty squares '.'.

When the rook moves, it chooses one of four cardinal directions (north, east, south, or west), then moves in that direction until it chooses to stop, reaches the edge of the board, captures a black pawn, or is blocked by a white bishop. A rook is considered attacking a pawn if the rook can capture the pawn on the rook's turn. The number of available captures for the white rook is the number of pawns that the rook is attacking.

Return the number of available captures for the white rook.

Example:

Input: board = [[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".","R",".",".",".","p"],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]]
Output: 3
Explanation: In this example, the rook is attacking all the pawns.

Approach:

C++

#include <bits/stdc++.h>
using namespace std;

int numRookCaptures(vector<vector<char>> &board)
{
    int xy;
    bool flag = false;
    int r = board.size();
    int c = board[0].size();

    // find the position of rook
    //on the board
    for (int i = 0i < ri++)
    {
        for (int j = 0j < cj++)
        {

            //if current position
            //on the board is rook
            //then store the x coordinates
            //y coordinate and break
            if (board[i][j] == 'R')
            {
                x = i;
                y = j;
                flag = true;
                break;
            }
        }
        if (flag)
            break;
    }

    int cnt = 0;
    bool B = falseP = false;
    // iterate x direction and find proper pawns
    for (int i = 0i < ci++)
    {
        if (i <= y)
        {

            //if pawns found
            if (board[x][i] == 'p')
                P = true;
            if (board[x][i] == 'B' && P)
                P = false;
            if (i == y && P)
            {
                cnt++;
                P = false;
            }
        }
        else
        {
            if (board[x][i] == 'B')
                B = true;
            if (board[x][i] == 'p' && !B)
                P = true;
            if (i == c - 1 && P)
            {
                cnt++;
            }
        }
    }
    // iterate in y direction and find the proper pawns
    B = falseP = false;
    for (int i = 0i < ri++)
    {
        if (i <= x)
        {
            if (board[i][y] == 'p')
                P = true;
            if (board[i][y] == 'B' && P)
                P = false;
            if (i == x && P)
            {
                cnt++;
                P = false;
            }
        }
        else
        {
            if (board[i][y] == 'B')
                B = true;
            if (board[i][y] == 'p' && !B)
                P = true;
            if (P && i == r - 1)
            {
                cnt++;
            }
        }
    }
    // a little long but so fast ...
    return cnt;
}

int main()
{
    vector<vector<char>> board = {{'.''.''.''.''.''.''.''.'},
                                  {'.''.''.''p''.''.''.''.'},
                                  {'.''.''.''R''.''.''.''p'},
                                  {'.''.''.''.''.''.''.''.'},
                                  {'.''.''.''.''.''.''.''.'},
                                  {'.''.''.''p''.''.''.''.'},
                                  {'.''.''.''.''.''.''.''.'},
                                  {'.''.''.''.''.''.''.''.'}};

    cout << numRookCaptures(board);

    return 0;
}


No comments:

Post a Comment