How to Compute the Number of Pawns-Captures for Rook in Chess?

  • 时间:2020-10-11 16:01:36
  • 分类:网络文摘
  • 阅读:123 次

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:

chessboard-1 How to Compute the Number of Pawns-Captures for Rook in Chess? c / c++ tutorial

Available Captures for Rook

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

Output: 3
Explanation:
In this example the rook is able to capture all the pawns.

Example 2:

chessboard-2 How to Compute the Number of Pawns-Captures for Rook in Chess? c / c++ tutorial

Available Captures for Rook

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:

chessboard-3 How to Compute the Number of Pawns-Captures for Rook in Chess? c / c++ tutorial

Available Captures for Rook

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.

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’

Chess Algorithm to compute Available Caputres for Rook in C++

The algorithm is straightforward: iterate each pixel on the chess board which is constant 8×8, as there is only one rook, we can break if we found it.

When we have the position of the Rook, we can move the Rook at four directions (straight and break if the rook meets Bishop, increment the counter if the rook meets a prawn).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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') { // found the ROOK
                   for (int x = i + 1; x < 8 && (board[x][j] != 'B'); ++ x) {
                       if (board[x][j] == 'p') {
                           ans ++;
                           break;
                       }
                   }
                   for (int x = i - 1; x >= 0 && (board[x][j] != 'B'); -- x) {
                       if (board[x][j] == 'p') {
                           ans ++;
                           break;
                       }
                   }                    
                   for (int x = j + 1; x < 8 && (board[i][x] != 'B'); ++ x) {
                       if (board[i][x] == 'p') {
                           ans ++;
                           break;
                       }
                   }                    
                   for (int x = j - 1; x >= 0 && (board[i][x] != 'B'); -- x) {
                       if (board[i][x] == 'p') {
                           ans ++;
                           break;
                       }
                   }
                   break; // as there is only 1 Rook.                                        
                }
            }
        }
        return ans;
    }
};
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') { // found the ROOK
                   for (int x = i + 1; x < 8 && (board[x][j] != 'B'); ++ x) {
                       if (board[x][j] == 'p') {
                           ans ++;
                           break;
                       }
                   }
                   for (int x = i - 1; x >= 0 && (board[x][j] != 'B'); -- x) {
                       if (board[x][j] == 'p') {
                           ans ++;
                           break;
                       }
                   }                    
                   for (int x = j + 1; x < 8 && (board[i][x] != 'B'); ++ x) {
                       if (board[i][x] == 'p') {
                           ans ++;
                           break;
                       }
                   }                    
                   for (int x = j - 1; x >= 0 && (board[i][x] != 'B'); -- x) {
                       if (board[i][x] == 'p') {
                           ans ++;
                           break;
                       }
                   }
                   break; // as there is only 1 Rook.                                        
                }
            }
        }
        return ans;
    }
};

Both time and space complexity is O(1).

–EOF (The Ultimate Computing & Technology Blog) —

推荐阅读:
权威发布:常见的致癌食物你吃过几种  “最大份扬州炒饭”喂猪背后的浮躁心态  吉尼斯宣布“最大份扬州炒饭”纪录无效  包菜有意想不到的防癌养胃保健功效  食药总局公布不合格食品名单蜂蜜上黑榜  板栗养胃健脾是医药学家推崇的补肾果  冬季手脚冰凉者可以多喝“三红”暖身汤  老百姓对于食物中致癌物的认识误区  三类食品是引发癌症(恶性肿瘤)的因素  枸杞虽好但两种人吃了反而对健康有害 
评论列表
添加评论