Available Captures for Rook - Array - Easy - LeetCode
💻 coding

Available Captures for Rook - Array - Easy - LeetCode

4 min read 769 words
4 min read
ShareWhatsAppPost on X
  • 1The rook can capture black pawns by moving in cardinal directions until blocked by bishops or the edge of the board.
  • 2The number of pawns the rook can capture depends on their positions relative to the rook and any blocking bishops.
  • 3The solution involves iterating through the board to identify the rook's position and calculating potential captures in all four directions.

AI-generated summary · May not capture all nuances

Key Insight
AskGif

"The rook can capture black pawns by moving in cardinal directions until blocked by bishops or the edge of the board."

Available Captures for Rook - Array - Easy - LeetCode

On an 8 x 8 chessboard, there is one white rook. There also may be empty squares, white bishops, and black pawns. These are given as characters 'R', '.', 'B', and 'p' respectively. Uppercase characters represent white pieces, and lowercase characters represent black pieces.

The rook moves as in the rules of Chess: it chooses one of four cardinal directions (north, east, west, and south), then moves in that direction until it chooses to stop, reaches the edge of the board, or captures an opposite colored pawn by moving to the same square it occupies. Also, rooks cannot move into the same square as other friendly bishops.

Return the number of pawns the rook can capture in one move.

Example 1:

Input: [[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".","R",".",".",".","p"],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]]

Output: 3

Explanation:

In this example the rook is able to capture all the pawns.

Example 2:

Input: [[".",".",".",".",".",".",".","."],[".","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",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]]

Output: 0

Explanation:

Bishops are blocking the rook to capture any pawn.

Example 3:

Input: [[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".","p",".",".",".","."],["p","p",".","R",".","p","B","."],[".",".",".",".",".",".",".","."],[".",".",".","B",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".",".",".",".",".","."]]

Output: 3

Explanation:

The rook can capture the pawns at positions b5, d6 and f5.

Note:

board.length == board[i].length == 8

board[i][j] is either 'R', '.', 'B', or 'p'

There is exactly one cell with board[i][j] == 'R'

Solution:

using System;
using System.Collections.Generic;
using System.Text;

namespace LeetCode.AskGif.Easy.Array
{
 public class NumRookCapturesSoln
 {
 public int NumRookCaptures(char[][] board)
 {
 var whiteRook = 'R'; 

 for (int i = 0; i < board.Length; i++)
 {
 for (int j = 0; j < board[i].Length; j++)
 {
 //check for White Rook
 if(board[i][j]== whiteRook)
 {
 return CalculatePawnsCount(board, i, j);
 }
 }
 }

 return 0;
 }

 private int CalculatePawnsCount(char[][] board, int row, int column)
 {
 var whiteBishop = 'B';
 var blackPawn = 'p';
 var empty = '.';

 int count = 0;

 //UP Count
 for (int i = row-1; i >= 0 && row != 0; i--)
 {
 if(board[i][column] != empty)
 {
 if( board[i][column] == blackPawn)
 {
 count += 1; 
 }
 break;
 }
 }

 //Down Count
 for (int i = row + 1; i < board.Length && row != board.Length-1; i++)
 {
 if (board[i][column] != empty)
 {
 if (board[i][column] == blackPawn)
 {
 count += 1;
 }
 break;
 }
 }

 //Left Count
 for (int i = column - 1; i >= 0 && column != 0; i--)
 {
 if (board[row][i] != empty)
 {
 if (board[row][i] == blackPawn)
 {
 count += 1;
 }
 break;
 }
 }

 //Right Count
 for (int i = column + 1; i < board[row].Length && column != board[row].Length-1; i++)
 {
 if (board[row][i] != empty)
 {
 if (board[row][i] == blackPawn)
 {
 count += 1;
 }
 break;
 }
 }

 return count;
 }
 }
}

Time Complexity: O(m*n) Where m and n are rows and columns of the matrix

Space Complexity: O(1)

Unit Tests:

using LeetCode.AskGif.Easy.Array;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Text;

namespace CodingUnitTest.Easy.Array
{
 [TestClass]
 public class NumRookCapturesSolnTests
 {
 [TestMethod]
 public void NumRookCapturesSoln_First()
 {
 var board = new string[,]{
 {".", ".", ".", ".", ".", ".", ".", "."},
 {".", ".", ".", "p", ".", ".", ".", "."},
 {".", ".", ".", "R", ".", ".", ".", "p"},
 {".", ".", ".", ".", ".", ".", ".", "."},
 {".", ".", ".", ".", ".", ".", ".", "."},
 {".", ".", ".", "p", ".", ".", ".", "."},
 {".", ".", ".", ".", ".", ".", ".", "."},
 { ".", ".", ".", ".", ".", ".", ".", "."}
 };
 var expected = 3;

 var res = new NumRookCapturesSoln().NumRookCaptures(ArrayMapper(board));
 Assert.AreEqual(expected, res);
 }

 [TestMethod]
 public void NumRookCapturesSoln_Second()
 {
 var board = new string[,]{
 {".", ".", ".", ".", ".", ".", ".", "."},
 {".", "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", ".", "."},
 {".", ".", ".", ".", ".", ".", ".", "."},
 { ".", ".", ".", ".", ".", ".", ".", "."}
 };
 var expected = 0;

 var res = new NumRookCapturesSoln().NumRookCaptures(ArrayMapper(board));
 Assert.AreEqual(expected, res);
 }

 [TestMethod]
 public void NumRookCapturesSoln_Third()
 {
 var board = new string[,]{
 {".", ".", ".", ".", ".", ".", ".", "."},
 {".", ".", ".", "p", ".", ".", ".", "."},
 {".", ".", ".", "p", ".", ".", ".", "."},
 {"p", "p", ".", "R", ".", "p", "B", "."},
 {".", ".", ".", ".", ".", ".", ".", "."},
 {".", ".", ".", "B", ".", ".", ".", "."},
 {".", ".", ".", "p", ".", ".", ".", "."},
 { ".", ".", ".", ".", ".", ".", ".", "."}
 };
 var expected = 3;

 var res = new NumRookCapturesSoln().NumRookCaptures(ArrayMapper(board));
 Assert.AreEqual(expected, res);
 }

 private char[][] ArrayMapper(string[,] matrix)
 {
 var arr = new char[matrix.GetLength(0)][];
 for (int i = 0; i < matrix.GetLength(0); i++)
 {
 arr[i] = new char[matrix.GetLength(1)];
 for (int j = 0; j < matrix.GetLength(1); j++)
 {
 arr[i][j] = matrix[i, j].ToCharArray()[0];
 }
 }

 return arr;
 }
 }
}

Enjoyed this article?

Share it with someone who'd find it useful.

ShareWhatsAppPost on X

AskGif

Published on 21 June 2020 · 4 min read · 769 words

Part of AskGif Blog · coding

You might also like