Algorithm 本周的算法题为 999. 可以被一步捕获的棋子数
在一个 8 x 8 的棋盘上,有一个白色的车(Rook),用字符 'R' 表示。棋盘上还可能存在空方块,白色的象(Bishop)以及黑色的卒(pawn),分别用字符 '.','B' 和 'p' 表示。不难看出,大写字符表示的是白棋,小写字符表示的是黑棋。
车按国际象棋中的规则移动。东,西,南,北四个基本方向任选其一,然后一直向选定的方向移动,直到满足下列四个条件之一:
棋手选择主动停下来。 棋子因到达棋盘的边缘而停下。 棋子移动到某一方格来捕获位于该方格上敌方(黑色)的卒,停在该方格内。 车不能进入/越过已经放有其他友方棋子(白色的象)的方格,停在友方棋子前。 你现在可以控制车移动一次,请你统计有多少敌方的卒处于你的捕获范围内(即,可以被一步捕获的棋子数)。
输入:[ [".",".",".",".",".",".",".","."], [".",".",".","p",".",".",".","."], [".",".",".","R",".",".",".","p"], [".",".",".",".",".",".",".","."], [".",".",".",".",".",".",".","."], [".",".",".","p",".",".",".","."], [".",".",".",".",".",".",".","."], [".",".",".",".",".",".",".","."] ] 输出:3 解释:在本例中,车能够捕获所有的卒。 实现代码如下:
var numRookCaptures = function (board) { var x = 0 var y = 0 // 定义标记outer,用于break中止外层for循环,结束获取R位置的流程。 outer: for (let i = 0; i < board.length; i++) { for (let j = 0; j < board[i].length; j++) { if (board[i][j] == 'R') { x = j y = i break outer } } } let count = 0 for (let i = x; i >= 0; i--) { if (board[y][i] === 'B') { break } if (board[y][i] === 'p') { count += 1 break } } for (let i = x; i < 8; i++) { if (board[y][i] === 'B') { break } if (board[y][i] === 'p') { count += 1 break } } for (let i = y; i >= 0; i--) { if (board[i][x] === 'B') { break } if (board[i][x] === 'p') { count += 1 break } } for (let i = y; i < 8; i++) { if (board[i][x] === 'B') { break } if (board[i][x] === 'p') { count += 1 break } } return count } 解题思路:...