问题:,两个皇后在棋盘上不能互相攻击的位置总数是多少?
方法:,我的逻辑是创建两个皇后的变量,它提供了棋盘上存在的位置的坐标。从那里开始,移动一个皇后(白色皇后)穿过板上的每一个打开位置,在每个新位置循环中,通过验证另一个女王(黑皇后)是否可以攻击它,首先确认它们是否在同一列或同一列。然后,对于从对角线向下开始的对角线攻击,使用攻击坐标变量( vx和vy值)向下移动,并在每次检查时向右移动,以查看它是否在对角线移动时,黑色皇后在出线之前是否与白色皇后具有相同的坐标。在每个验证路径之后,变量vx和vy被故意设置为a -1,这样它就不会意外地触发一个验证,验证是基于循环完成时它停止的次数。
,问题是:代码一直工作到//验证对角线向下/右攻击部分(39+行)为止。作为真正的声明,我所放的内容似乎并不重要,结果冻结了应用程序,并迫使我使用break命令退出它。如果我注释掉对角线攻击部分,程序(原样)将运行并返回一个值49。我正试图得到42的答案。当黑皇后位于左上角(0,0)位置时,7是对角线向下/右攻击模式的区别。
曾经解决过:我知道数组会更容易一些,但是在这一点上,我知道有一些变化会解除这个障碍,如果有人能告诉我我缺少的是什么,那么其他三个对角线攻击模式就是复制/粘贴对角线攻击部分,然后更改变量的+和-符号,以改变攻击皇后的对角线运动。从那里开始,每次白人女王在黑皇后的新坐标上迭代时,都会加入黑色皇后移动的时间循环。
你的帮助将不胜感激。非常感谢你抽出时间来!
<!DOCTYPE html>
<html>
<body>
<h2>Answer To Two Queens Question</h2>
<p id="demo"></p>
<script>
//total number of positions the two queens cannot attack each other
var n = 0;
//white queen
var x1 = 0;
var y1 = 0;
//black queen
var x2 = 0;
var y2 = 0;
//validation attack path
var vx = -1;
var vy = -1;
//moves white queen down one row after iterating through all available options
while (y1 <= 7) {
//moves white queen across an entire row
while (x1 <= 7) {
//skips validation of attack patterns if queens are on the same square on the board (invalid scenario)
if (x1==x2 && y1==y2){
x1 = x1 + 1;
//validates horizontal attack (left & right)
} else if (x1 == x2) {
x1 = x1 + 1;
//validates vertical attack (up & down)
} else if (y1 == y2){
x1 = x1 + 1;
//validates diagonal down/right attack
} else if (1 == 1) {
//sets validation coordinates to be the same as the black queen
vx = x2;
vy = y2;
//iterates through diagonal down/right attack coordinates to see if it matches same coordinates of white queen
while(vx <= 7 && vy <= 7){
if (x2 + vx == x1 && y2 + vy == y1) {
x1 = x1 + 1;
} else {
vx = vx + 1;
vy = vy + 1;
}
}
vx = -1;
vy = -1;
//if all validations fail, count towards total count where they cannot attack
} else {
n = n + 1;
x1 = x1 + 1;
}
}
y1 = y1 + 1;
x1 = 0;
}
document.getElementById("demo").innerHTML = "The number positions where two queens cannot attack each other is " + n;
</script>
</body>
</html>
发布于 2021-01-21 18:12:39
作为注释的延续,该解决方案将“位置”视为介于0到63之间的整数,其中行和列可以使用“算术级数”逻辑导出。一列可由8除法的地板部分导出,一列可由8上的模数导出。
这样只需要两个循环。采用双回路,使每个Queen1 (Q1)位置与Q2的位置相比较。正如您的alg指定的那样,当Q1和Q2占用相同的方格时,有一个条件可以排除。其余的逻辑将在代码注释中解释。
与您的算法显著不同的是,此解决方案试图解决何时可以进行攻击。返回值必须处理时,位置不能作出,通过扣减。
免责声明:这可能不是解决方案,因为OP事先不知道正确的返回值。这只是一次代码尝试.
// What is the total number of positions that two queens can exist on a chess board where they cannot attack each other?
function answer() {
var Q1;
var Q2;
var cnt = 0; // count of when they can attack each other
for (Q1 = 0; Q1 < 64; Q1++) {
//console.log(Q1);
let Q1_y = Math.floor(Q1 / 8); // 0..7 ~ 0, 8..15 ~ 1, and so on
let Q1_x = Q1 % 8;
// console.log(Q1_x + ", " + Q1_y);
for (Q2 = 0; Q2 < 64; Q2++) {
let Q2_y = Math.floor(Q2 / 8); // 0..7 ~ 0, 8..15 ~ 1, and so on
let Q2_x = Q2 % 8;
if (Q1 != Q2) {
// rule out both on same square
let canAttack = false;
// Now determine if on same X
if (Q1_x == Q2_x) {
canAttack = true;
}
// Now determine if on same Y
if (Q1_y == Q2_y) {
canAttack = true;
}
// Now determine if on same diagnoal
let diag = (Math.abs(Q1_x - Q2_x) / Math.abs(Q1_y - Q2_y)) == 1;
if (diag) {
canAttack = true;
}
// Update count for this square combo
if (canAttack) {
cnt++;
}
}
}
}
// console.log (cnt);
return ((64 * 64) - 64) - cnt; // 64*64 total space pairs, minus the 64 times they are on same square, minus the number of times they *can* attack
}
console.log (answer());
发布于 2021-01-22 17:18:54
为了好玩,下面是关于这个问题的另一个看法。
在这种情况下,使用标准的方式使用比特板进行国际象棋编程,以表示棋子的可能移动。也就是说,一个Uint64数字被用作一个位掩码,用来表示一辆车、主教或王后的可能动作。由于板上有64个正方形,因此需要64个Uint64位掩码,以表示每个方块上的可能移动。
为了帮助可视化,下面的脚本打印出方框'd3‘的鲁克、主教和女王比特板(这是块数组中64个方块中的第19个)。
一旦建立了比特板,确定两个皇后是否互相攻击的努力是微不足道的,因为这只是一个逻辑上的ANDing第一个女王的移动位板与第二个女王所坐的方块代表的位板的问题,如果结果是0,那么它们就不是互相攻击。
顺便说一句,这种方法可以达到2576对不互相攻击的皇后的组合。
// Establish array with each bitboard representing the square on the board
// that the bitboard index represents.
let squares = new BigUint64Array( 64 ).map( ( v , i ) => 1n << BigInt( i ) );
// Generate possible moves for Rooks and Bishops for each square
// that they can occupy.
let rookMoves = new BigUint64Array( 64 );
let bishopMoves = new BigUint64Array( 64 );
for ( pieceRank = 0; pieceRank < 8; pieceRank++ ) {
for ( pieceFile = 0; pieceFile < 8; pieceFile++ ) {
let squareIndex = pieceRank * 8 + pieceFile;
let rookMap = 0n;
let bishopMap = 0n;
for ( let r = 0; r < 8; r++ ) {
for ( let f = 0; f < 8; f++ ) {
let rf = r * 8 + f;
if ( ( r === pieceRank || f === pieceFile ) && rf !== squareIndex ) {
rookMap |= squares[ rf ];
}
if ( ( Math.abs( r - pieceRank ) === Math.abs( f - pieceFile ) ) && rf !== squareIndex ) {
bishopMap |= squares[ rf ];
}
}
}
rookMoves[ squareIndex ] = rookMap;
bishopMoves[ squareIndex ] = bishopMap;
}
}
// Generate the possible moves for Queens for each square that
// they can occupy. This is done my looping through the Rook
// moves and simply ORing with the bishop moves.
let queenMoves = rookMoves.map( ( v, i ) => v | bishopMoves[ i ] );
// Helper function to print out the bitmap as a board, where 'X'
// represents a bit that is set to 1 and '-' a bit that is set to '0'.
function printBitMap( bitBoard ) {
let bm = '';
for ( rank = 7; 0 <= rank; rank-- ) {
for ( file = 0; file < 8; file++ ) {
bm += bitBoard & squares[ rank * 8 + file ] ? ' X ' : ' - ';
}
bm += '\n';
}
return bm + '\n';
}
// Let's print the bitmaps for the rook, bishop, and queen at square d3,
// which is rank 2 ( range 0 - 7 ) file 3, or 2 * 8 + 3 which is square
// index 19.
console.log( 'Rook, Bishop, and Queen moves from square d3:' );
console.log( printBitMap( rookMoves[ 19 ] ) );
console.log( printBitMap( bishopMoves[ 19 ] ) );
console.log( printBitMap( queenMoves[ 19 ] ) );
// Now that the bitmaps are set up, the effort is simple... Loop through
// all combinations of two queens on the board...
let nonIntersectCount = 0;
for ( let Q1square = 0; Q1square < 64; Q1square++ ) {
for ( let Q2square = 0; Q2square < 64; Q2square++ ) {
// ...and if the queens are not on the same square and the intersection of
// the possible moves for Q1 with the Q2 square is 0 bits, then we have
// a situation where the queens are not attacking each other...
if ( Q1square !== Q2square && ( queenMoves[ Q1square ] & squares[ Q2square ] ) === 0n ) {
nonIntersectCount++;
}
}
}
console.log( 'For every combination of 2 queens on a board, the number of combinations where they do not attack each other is:' );
console.log( nonIntersectCount );
还请注意,截至2021年1月,BigUint64Array具有广泛的浏览器支持,但Safari除外。在这种情况下,如果Safari支持对于当前的问题至关重要,那么人们可能会考虑使用Uint32Array重新处理这个解决方案.
https://stackoverflow.com/questions/65836721
复制相似问题