【LEETCODE】46、999. Available Captures for Rook

package y2019.Algorithm.array;

/**
 * @ProjectName: cutter-point
 * @Package: y2019.Algorithm.array
 * @ClassName: NumRookCaptures
 * @Author: xiaof
 * @Description: 999. Available Captures for Rook
 *
 * 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.
 *
 * Input: [[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".","R",".",".",".","p"],
 * [".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],
 * [".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]]
 * Output: 3
 * Explanation:
 * In this example the rook is able to capture all the pawns.
 *
 * 在一个国际象棋的棋盘上,有一个白车(R),有若干白象(B)、黑卒(p),其余是空白(.),问这个白车在只移动一次的情况下,能吃掉哪几个黑卒
 *
 * @Date: 2019/7/4 9:20
 * @Version: 1.0
 */
public class NumRookCaptures {

    public int solution(char[][] board) {
        //因为白车是横向和纵向都可以移动全部位置,那么我们只需要判断四个方向就可以知道能吃几个了
        //第一步肯定是定位到白车R
        int row = 0, column = 0;
        for(int i = 0; i < board.length; ++i) {
            for(int j = 0; j < board[i].length; ++j) {
                if(board[i][j] == 'R') {
                    row = i;
                    column = j;
                    break;
                }
            }
        }

        //然后根据白车的位置横向或纵向比那里获取黑卒个数
        int result = 0;
        //横
        for(int index1 = column - 1, index2 = column + 1; index1 >= 0 || index2 < board[row].length; ++index2, --index1) {
            if(index1 >= 0 && board[row][index1] == 'p') {
                result++;
                //并且只能吃一次
                index1 = -1;
            } else if (index1 >= 0 && board[row][index1] == 'B') {
                index1 = -1; //如果遇到B,白象那么就停下
            }

            if(index2 < board[row].length && board[row][index2] == 'p') {
                result++;
                index2 = board[row].length;
            } else if (index2 < board[row].length && board[row][index2] == 'B') {
                index2 = board[row].length; //如果遇到B,白象那么就停下
            }
        }

        //纵向
        for(int index1 = row - 1, index2 = row + 1; index1 >= 0 || index2 < board.length; ++index2, --index1) {
            if(index1 >= 0 && board[index1][column] == 'p') {
                result++;
                //并且只能吃一次
                index1 = -1;
            } else if (index1 >= 0 && board[index1][column] == 'B') {
                index1 = -1; //如果遇到B,白象那么就停下
            }

            if(index2 < board.length && board[index2][column] == 'p') {
                result++;
                index2 = board[row].length;
            } else if (index2 < board.length && board[index2][column] == 'B') {
                index2 = board.length; //如果遇到B,白象那么就停下
            }
        }

        return result;
    }


    public static void main(String args[]) {
        char A1[][] = {{'.','.','.','.','.','.','.','.'},{'.','.','.','p','.','.','.','.'},{'.','.','.','p','.','.','.','.'},{'p','p','.','R','.','p','B','.'},{'.','.','.','.','.','.','.','.'},{'.','.','.','B','.','.','.','.'},{'.','.','.','p','.','.','.','.'},{'.','.','.','.','.','.','.','.'}};
        NumRookCaptures fuc = new NumRookCaptures();
        System.out.println(fuc.solution(A1));
    }

}

 

上一篇:STL练习(二):代码对齐,Ducci序列,卡片游戏


下一篇:剑指Offer(牛客版)--面试题11:旋转数组的最小数字