Ai
1 Star 0 Fork 81

zhizou/javascript-algorithms

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
QueenPosition.js 1.07 KB
一键复制 编辑 原始数据 按行查看 历史
Oleksii Trekhleb 提交于 2018-05-16 12:54 +08:00 . Add N-Queens.
/**
* Class that represents queen position on the chessboard.
*/
export default class QueenPosition {
/**
* @param {number} rowIndex
* @param {number} columnIndex
*/
constructor(rowIndex, columnIndex) {
this.rowIndex = rowIndex;
this.columnIndex = columnIndex;
}
/**
* @return {number}
*/
get leftDiagonal() {
// Each position on the same left (\) diagonal has the same difference of
// rowIndex and columnIndex. This fact may be used to quickly check if two
// positions (queens) are on the same left diagonal.
// @see https://youtu.be/xouin83ebxE?t=1m59s
return this.rowIndex - this.columnIndex;
}
/**
* @return {number}
*/
get rightDiagonal() {
// Each position on the same right diagonal (/) has the same
// sum of rowIndex and columnIndex. This fact may be used to quickly
// check if two positions (queens) are on the same right diagonal.
// @see https://youtu.be/xouin83ebxE?t=1m59s
return this.rowIndex + this.columnIndex;
}
toString() {
return `${this.rowIndex},${this.columnIndex}`;
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/zhizous/javascript-algorithms.git
git@gitee.com:zhizous/javascript-algorithms.git
zhizous
javascript-algorithms
javascript-algorithms
master

搜索帮助