|
| 1 | +package com.fishercoder.solutions; |
| 2 | + |
| 3 | +/** |
| 4 | + * 999. Available Captures for Rook |
| 5 | + * |
| 6 | + * On an 8 x 8 chessboard, there is one white rook. |
| 7 | + * There also may be empty squares, white bishops, and black pawns. |
| 8 | + * These are given as characters 'R', '.', 'B', and 'p' respectively. |
| 9 | + * Uppercase characters represent white pieces, and lowercase characters represent black pieces. |
| 10 | + * |
| 11 | + * The rook moves as in the rules of Chess: |
| 12 | + * it chooses one of four cardinal directions (north, east, west, and south), |
| 13 | + * then moves in that direction until it chooses to stop, reaches the edge of the board, |
| 14 | + * or captures an opposite colored pawn by moving to the same square it occupies. |
| 15 | + * Also, rooks cannot move into the same square as other friendly bishops. |
| 16 | + * |
| 17 | + * Return the number of pawns the rook can capture in one move. |
| 18 | + * |
| 19 | + * Example 1: |
| 20 | + * |
| 21 | + * Input:[ |
| 22 | + * 8 [".",".",".",".",".",".",".","."], |
| 23 | + * 7 [".",".",".","p",".",".",".","."], |
| 24 | + * 6 [".",".",".","R",".",".",".","p"], |
| 25 | + * 5 [".",".",".",".",".",".",".","."], |
| 26 | + * 4 [".",".",".",".",".",".",".","."], |
| 27 | + * 3 [".",".",".","p",".",".",".","."], |
| 28 | + * 2 [".",".",".",".",".",".",".","."], |
| 29 | + * 1 [".",".",".",".",".",".",".","."]] |
| 30 | + * a b c d e f g h |
| 31 | + * |
| 32 | + * Output: 3 |
| 33 | + * Explanation: |
| 34 | + * In this example the rook is able to capture all the pawns. |
| 35 | + * |
| 36 | + * Example 2: |
| 37 | + * |
| 38 | + * Input:[ |
| 39 | + * 8 [".",".",".",".",".",".",".","."], |
| 40 | + * 7 [".","p","p","p","p","p",".","."], |
| 41 | + * 6 [".","p","p","B","p","p",".","."], |
| 42 | + * 5 [".","p","B","R","B","p",".","."], |
| 43 | + * 4 [".","p","p","B","p","p",".","."], |
| 44 | + * 3 [".","p","p","p","p","p",".","."], |
| 45 | + * 2 [".",".",".",".",".",".",".","."], |
| 46 | + * 1 [".",".",".",".",".",".",".","."]] |
| 47 | + * a b c d e f g h |
| 48 | + * |
| 49 | + * Output: 0 |
| 50 | + * Explanation: |
| 51 | + * Bishops are blocking the rook to capture any pawn. |
| 52 | + * |
| 53 | + * Example 3: |
| 54 | + * |
| 55 | + * Input: [ |
| 56 | + * 8 [".",".",".",".",".",".",".","."], |
| 57 | + * 7 [".",".",".","p",".",".",".","."], |
| 58 | + * 6 [".",".",".","p",".",".",".","."], |
| 59 | + * 5 ["p","p",".","R",".","p","B","."], |
| 60 | + * 4 [".",".",".",".",".",".",".","."], |
| 61 | + * 3 [".",".",".","B",".",".",".","."], |
| 62 | + * 2 [".",".",".","p",".",".",".","."], |
| 63 | + * 1 [".",".",".",".",".",".",".","."]] |
| 64 | + * a b c d e f g h |
| 65 | + * |
| 66 | + * Output: 3 |
| 67 | + * Explanation: |
| 68 | + * The rook can capture the pawns at positions b5, d6 and f5. |
| 69 | + * |
| 70 | + * Note: |
| 71 | + * board.length == board[i].length == 8 |
| 72 | + * board[i][j] is either 'R', '.', 'B', or 'p' |
| 73 | + * There is exactly one cell with board[i][j] == 'R' |
| 74 | + */ |
| 75 | +public class _999 { |
| 76 | + public static class Solution1 { |
| 77 | + int[] directions = new int[] {0, 1, 0, -1, 0}; |
| 78 | + |
| 79 | + public int numRookCaptures(char[][] board) { |
| 80 | + int m = board.length; |
| 81 | + int n = board[0].length; |
| 82 | + int rowR = -1; |
| 83 | + int colR = -1; |
| 84 | + for (int i = 0; i < m; i++) { |
| 85 | + for (int j = 0; j < n; j++) { |
| 86 | + if (board[i][j] == 'R') { |
| 87 | + rowR = i; |
| 88 | + colR = j; |
| 89 | + break; |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + int count = 0; |
| 94 | + for (int i = 0; i < 4; i++) { |
| 95 | + int neighborRow = rowR + directions[i]; |
| 96 | + int neighborCol = colR + directions[i + 1]; |
| 97 | + if (neighborRow >= 0 && neighborRow < m |
| 98 | + && neighborCol >= 0 && neighborCol < n |
| 99 | + && board[neighborRow][neighborCol] != 'B') { |
| 100 | + if (directions[i] == 0 && directions[i + 1] == 1) { |
| 101 | + while (neighborCol < n) { |
| 102 | + if (board[neighborRow][neighborCol] == 'p') { |
| 103 | + count++; |
| 104 | + break; |
| 105 | + } else if (board[neighborRow][neighborCol] == 'B') { |
| 106 | + break; |
| 107 | + } else { |
| 108 | + neighborCol++; |
| 109 | + } |
| 110 | + } |
| 111 | + } else if (directions[i] == 1 && directions[i + 1] == 0) { |
| 112 | + while (neighborRow < m) { |
| 113 | + if (board[neighborRow][neighborCol] == 'p') { |
| 114 | + count++; |
| 115 | + break; |
| 116 | + } else if (board[neighborRow][neighborCol] == 'B') { |
| 117 | + break; |
| 118 | + } else { |
| 119 | + neighborRow++; |
| 120 | + } |
| 121 | + } |
| 122 | + } else if (directions[i] == 0 && directions[i + 1] == -1) { |
| 123 | + while (neighborCol >= 0) { |
| 124 | + if (board[neighborRow][neighborCol] == 'p') { |
| 125 | + count++; |
| 126 | + break; |
| 127 | + } else if (board[neighborRow][neighborCol] == 'B') { |
| 128 | + break; |
| 129 | + } else { |
| 130 | + neighborCol--; |
| 131 | + } |
| 132 | + } |
| 133 | + } else { |
| 134 | + while (neighborRow >= 0) { |
| 135 | + if (board[neighborRow][neighborCol] == 'p') { |
| 136 | + count++; |
| 137 | + break; |
| 138 | + } else if (board[neighborRow][neighborCol] == 'B') { |
| 139 | + break; |
| 140 | + } else { |
| 141 | + neighborRow--; |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + return count; |
| 149 | + } |
| 150 | + } |
| 151 | +} |
0 commit comments