JS:嵌套循环以获得对角线行和列?
JS: Nested loops to get diagonal rows and columns?
我对从嵌套循环中获取对角线数组的尝试感到有点困惑。
假设有一个 8 x 8 方格的棋盘,其中一个方格中有一个皇后。我可以完美地计算出皇后向上、向右、向下和向左可能的移动,但我被困在对角线移动上。
假设皇后在[4, 4] ([row, column])。我通过可能的对角线移动将此代码写入generate/loop,例如,左下。
r_q
是皇后在棋盘这一行的位置(从下到上),而c_q
是皇后在棋盘那一列的位置(从左到右) .
let dlArr = [];
let attackCount = 0;
let r_q = 4, c_q = 4;
for(let dl = r_q - 1; dl > 0; dl--) {
for(let ld = c_q - 1; ld > 0; ld--) {
dlArr.push([dl, ld]);
attackCount++;
}
}
console.log(dlArr);
我取回了 dlArr
的数组
[ [ 3, 3 ], [ 3, 2 ], [ 3, 1 ], [ 2, 3 ], [ 2, 2 ], [ 2, 1 ], [ 1, 3 ], [ 1, 2 ], [ 1, 1 ] ]
而不是应该的
[ [ 3, 3 ], [ 2, 2 ], [ 1, 1 ] ]
我写的这个嵌套循环哪里做错了?
我能看到的是它在进入第二个循环之前循环了第一个循环....
您需要检查两个点之间是否有相同的 slope
:
if(r_q-c_q === dl-ld)
所以:
[4,4],[3,3] 4-4 === 3-3
[4,4],[3,2] 4-4 !== 3-2
let dlArr = [];
let attackCount = 0;
let r_q = 4, c_q = 4;
for(let dl = r_q - 1; dl > 0; dl--) {
for(let ld = c_q - 1; ld > 0; ld--) {
if(r_q-c_q === dl-ld){
dlArr.push([dl, ld]);
attackCount++;
}
}
}
console.log(dlArr);
您不需要的输出返回正确方块的每个排列,因为您嵌套了两个 for
循环。相反,只需使用一个递减每个坐标的 for 循环。这应该给你女王西南斜对角的所有方块:
let dlArr = [];
let attackCount = 0;
let r_q = 4;
let c_q = 4;
let dl = (r_q - 1);
let ld = (c_q - 1);
while (dl > 0 && ld > 0) {
dlArr.push([dl, ld]);
attackCount++;
dl--;
ld--;
}
console.log(dlArr);
dl
不需要等同于 ld
。有很多位置不是这样。
编辑:我觉得这个 shorthand 版本很有趣:
let dlArr = [];
let attackCount = 0;
let r_q = 4;
let c_q = 4;
let dl = r_q; // no need for (r_q - 1)
let ld = c_q; // ditto
while ( (--dl && --ld) > 0 ) {
dlArr.push([dl, ld]);
attackCount++;
}
console.log(dlArr);
我对从嵌套循环中获取对角线数组的尝试感到有点困惑。
假设有一个 8 x 8 方格的棋盘,其中一个方格中有一个皇后。我可以完美地计算出皇后向上、向右、向下和向左可能的移动,但我被困在对角线移动上。
假设皇后在[4, 4] ([row, column])。我通过可能的对角线移动将此代码写入generate/loop,例如,左下。
r_q
是皇后在棋盘这一行的位置(从下到上),而c_q
是皇后在棋盘那一列的位置(从左到右) .
let dlArr = [];
let attackCount = 0;
let r_q = 4, c_q = 4;
for(let dl = r_q - 1; dl > 0; dl--) {
for(let ld = c_q - 1; ld > 0; ld--) {
dlArr.push([dl, ld]);
attackCount++;
}
}
console.log(dlArr);
我取回了 dlArr
[ [ 3, 3 ], [ 3, 2 ], [ 3, 1 ], [ 2, 3 ], [ 2, 2 ], [ 2, 1 ], [ 1, 3 ], [ 1, 2 ], [ 1, 1 ] ]
而不是应该的
[ [ 3, 3 ], [ 2, 2 ], [ 1, 1 ] ]
我写的这个嵌套循环哪里做错了?
我能看到的是它在进入第二个循环之前循环了第一个循环....
您需要检查两个点之间是否有相同的 slope
:
if(r_q-c_q === dl-ld)
所以:
[4,4],[3,3] 4-4 === 3-3
[4,4],[3,2] 4-4 !== 3-2
let dlArr = [];
let attackCount = 0;
let r_q = 4, c_q = 4;
for(let dl = r_q - 1; dl > 0; dl--) {
for(let ld = c_q - 1; ld > 0; ld--) {
if(r_q-c_q === dl-ld){
dlArr.push([dl, ld]);
attackCount++;
}
}
}
console.log(dlArr);
您不需要的输出返回正确方块的每个排列,因为您嵌套了两个 for
循环。相反,只需使用一个递减每个坐标的 for 循环。这应该给你女王西南斜对角的所有方块:
let dlArr = [];
let attackCount = 0;
let r_q = 4;
let c_q = 4;
let dl = (r_q - 1);
let ld = (c_q - 1);
while (dl > 0 && ld > 0) {
dlArr.push([dl, ld]);
attackCount++;
dl--;
ld--;
}
console.log(dlArr);
dl
不需要等同于 ld
。有很多位置不是这样。
编辑:我觉得这个 shorthand 版本很有趣:
let dlArr = [];
let attackCount = 0;
let r_q = 4;
let c_q = 4;
let dl = r_q; // no need for (r_q - 1)
let ld = c_q; // ditto
while ( (--dl && --ld) > 0 ) {
dlArr.push([dl, ld]);
attackCount++;
}
console.log(dlArr);