在一个 8 x 8 的棋盘上,有一个白色车(rook)。也可能有空方块,白色的象(bishop)和黑色的卒(pawn)。它们分别以字符 “R”,“.”,“B” 和 “p” 给出。大写字符表示白棋,小写字符表示黑棋。
车按国际象棋中的规则移动:它选择四个基本方向中的一个(北,东,西和南),然后朝那个方向移动,直到它选择停止、到达棋盘的边缘或移动到同一方格来捕获该方格上颜色相反的卒。另外,车不能与其他友方(白色)象进入同一个方格。
返回车能够在一次移动中捕获到的卒的数量。
示例 1:

输入:[[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".","R",".",".",".","p"],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]]
输出:3
解释:
在本例中,车能够捕获所有的卒。示例 2:

输入:[[".",".",".",".",".",".",".","."],[".","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",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."]]
输出:0
解释:
象阻止了车捕获任何卒。示例 3:

输入:[[".",".",".",".",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".","p",".",".",".","."],["p","p",".","R",".","p","B","."],[".",".",".",".",".",".",".","."],[".",".",".","B",".",".",".","."],[".",".",".","p",".",".",".","."],[".",".",".",".",".",".",".","."]]
输出:3
解释:
车可以捕获位置 b5,d6 和 f5 的卒。提示:
•board.length == board[i].length == 8•board[i][j] 可以是 'R','.','B' 或 'p'•只有一个格子上存在 board[i][j] == 'R'
没下过国际象棋,题目有读不懂,放弃!
其实,描述说的问题我们通过图来说明,以白色车为原点,向上下、左右试探。

所以我们首先找到白色车所在位置,然后遍历获取到 X 向、Y 向数组,并且过滤掉空白块,得到的数组我们只需要从第一个"p"开始找,到下一个"p"或者墙结束
/**
* @param {character[][]} board
* @return {number}
*/
var numRookCaptures = function(board) {
let y, xlist = [], ylist = [], boardXLen = 8;
for (let i = 0; i < boardXLen; i++) {
if (board[i].indexOf('R') > -1) {
y = board[i].indexOf('R')
xlist = board[i].filter(e => e !== '.')
}
}
for (let i = 0; i < boardXLen; i++) {
board[i][y] !== '.' && ylist.push(board[i][y])
}
let rx = xlist.indexOf('R'), ry = ylist.indexOf('R'), num = 0
if(xlist[rx+1] && xlist[rx+1] === 'p') ++num
if(xlist[rx-1] && xlist[rx-1] === 'p') ++num
if(ylist[ry+1] && ylist[ry+1] === 'p') ++num
if(ylist[ry-1] && ylist[ry-1] === 'p') ++num
return num
};•class 声明变量会提升,但不会初始化赋值。变量进入暂时性死区,类似于 let、const 声明
const p = new People(); // it's ok
function People() {
this.bar = 1;
}
const m = new Man(); // ReferenceError: Foo is not defined
class Man {
constructor() {
this.foo = 1;
}
}•class 声明内部会启用严格模式
function People() {
baz = 1; // it's ok
}
const p = new People();
class Man {
constructor() {
fol = 1; // ReferenceError: fol is not defined
}
}
const m = new Man();•class 的所有方法(包括静态方法和实例方法)是不可枚举
// 引用一个未声明的变量
function People() {
this.bar = 1;
}
People.say = function() {
return 1;
};
People.prototype.eat = function() {
// ...
};
const pKeys = Object.keys(Bar); // ['say']
const pProtoKeys = Object.keys(Bar.prototype); // ['eat']
class Man {
constructor() {
this.foo = 1;
}
static say() {
return 1;
}
eat() {
// ...
}
}
const mKeys = Object.keys(Man); // []
const mProtoKeys = Object.keys(Man.prototype); // []•class 的所有方法(包括静态方法和实例方法)都没有原型对象 prototype,所以也没有[[construct]],不能使用 new 来调用。
function People() {
this.bar = 1;
}
People.prototype.print = function() {
console.log(this.bar);
};
const p = new People();
const pPrint = new bar.print(); // it's ok
class Man {
constructor() {
this.foo = 42;
}
print() {
console.log(this.foo);
}
}
const m = new Man();
const mPrint = new m.print(); // TypeError: foo.print is not a constructor•必须使用 new 调用 class。
function People() {
this.bar = 1;
}
const p = People(); // it's ok
class Man {
constructor() {
this.foo = 1;
}
}
const m = Man(); // TypeError: Class constructor Foo cannot be invoked without 'new'•class 内部无法重写类名。
function People() {
People = 'Pap'; // it's ok
this.bar = 1;
}
const p = new People();
// People: 'Pap'
// bar: People {bar: 1}
class Man {
constructor() {
this.foo = 42;
Man = 'Woman'; // TypeError: Assignment to constant variable
}
}
const m = new Man();
Man = 'Fol'; // it's ok[1] 999. 车的可用捕获量: https://leetcode-cn.com/problems/available-captures-for-rook/