Javascript 如何在此框中检查多重密度阵列中的空值
您好,我想知道在方框3x3周围查找空值的算法 示例多维数组如下所示Javascript 如何在此框中检查多重密度阵列中的空值,javascript,loops,for-loop,matrix,multidimensional-array,Javascript,Loops,For Loop,Matrix,Multidimensional Array,您好,我想知道在方框3x3周围查找空值的算法 示例多维数组如下所示 [ [" ", " ", "#", "#", "#", " ", " ", " ", " ", " "], [" ", " ", "#", " ", "#", " ", " ", " ", " ", " "], [" ", " ", "#", "#", "#", " ", " ", " ", " ", " "], [" ", " ", " ", " ", " ", " ", " ", " ", "
[
[" ", " ", "#", "#", "#", " ", " ", " ", " ", " "],
[" ", " ", "#", " ", "#", " ", " ", " ", " ", " "],
[" ", " ", "#", "#", "#", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " ", " ", "#", "#"],
[" ", " ", " ", " ", " ", " ", " ", " ", "#", " "],
[" ", " ", " ", " ", " ", " ", " ", " ", "#", "#"],
[" ", " ", " ", " ", " ", " ", " ", " ", " ", " "]
]
我只想得到方框3x3,所以方框内的空值可以填充我想要的任何内容,如何解决/验证这种情况?
所以结果是得到空值索引
我正在尝试这样做:
for (let i = 0; i < array.length; i++) {
for (let j = 0; j < array[i].length; j++) {
if(array[i]){
if(array[i][j] == '#' &&
array[i+1][j] == '#' &&
array[i+2][j] == '#' &&
array[i][j+1] == '#' &&
array[i][j+2] == '#' &&
array[i+2][j+2]
){
console.log(i);
}
}
}
}
您可以搜索每行中的第一行,因为它是顶部边框。然后,一旦找到它,请验证该框的其余部分是否存在。如果有,则您已找到第一个3x3框: var arr=[ [ , , , , , , , ], [ , , , , , , , ], [ , , , , , , , ], [ , , , , , , , , , ], [ , , , , , , , , , ], [ , , , , , , , , ], [ , , , , , , , , ], [ , , , , , , , , ], [ , , , , , , , , , ] ]; 对于let i=0;i
如果您正在查找框内emtpy单元格的地址,则您正在查找i+1和j+1。您的思路正确,以下是您的版本,其中有一些小的更改和添加: 数组=[ [ , , , , , , , ], [,,找到我,,,,], [ , , , , , , , ], [ , , , , , , , , , ], [ , , , , , , , , , ], [ , , , , , , , , ], [ , , , , , , , , ], [ , , , , , , , , ], [ , , , , , , , , , ] ] 对于let i=0;i
const puzzle1 =
[ [" ", " ", "#", "#", "#", " ", " ", " ", " ", " "]
, [" ", " ", "#", " ", "#", " ", " ", " ", " ", " "]
, [" ", " ", "#", "#", "#", " ", " ", " ", " ", " "]
, [" ", " ", " ", " ", " ", " ", " ", " ", " ", " "]
, [" ", " ", " ", " ", " ", " ", " ", " ", " ", " "]
, [" ", " ", " ", " ", " ", " ", " ", " ", "#", "#"]
, [" ", " ", " ", " ", " ", " ", " ", " ", "#", " "]
, [" ", " ", " ", " ", " ", " ", " ", " ", "#", "#"]
, [" ", " ", " ", " ", " ", " ", " ", " ", " ", " "]
]
const puzzle2 =
[ [" ", " ", " ", " ", " ", " ", " ", " ", "#", "#"]
, [" ", " ", " ", " ", " ", " ", " ", " ", "#", " "]
, [" ", " ", " ", " ", " ", " ", " ", " ", "#", "#"]
, [" ", " ", " ", " ", "#", "#", "#", " ", " ", " "]
, [" ", " ", " ", " ", "#", " ", "#", " ", " ", " "]
, [" ", " ", " ", " ", "#", "#", "#", " ", " ", " "]
, [" ", " ", " ", " ", " ", " ", " ", " ", " ", " "]
, [" ", " ", " ", " ", " ", " ", " ", " ", " ", " "]
, [" ", " ", " ", " ", " ", " ", " ", " ", " ", " "]
]
console.log(check(puzzle1))
// expected: [ 0, 2 ]
console.log(check(puzzle2))
// expected: [ 3, 4 ]
一个否定的例子-
const puzzle3 =
[ [" ", " ", " ", " ", " ", " ", " ", " ", "#", "#"]
, [" ", " ", " ", " ", " ", " ", " ", " ", "#", " "]
, [" ", " ", " ", " ", " ", " ", " ", " ", "#", "#"]
, [" ", " ", " ", " ", "#", "#", "#", " ", " ", " "]
, [" ", " ", " ", " ", "#", "#", "#", " ", " ", " "]
, [" ", " ", " ", " ", "#", "#", "#", " ", " ", " "]
, [" ", " ", " ", " ", " ", " ", " ", " ", " ", " "]
, [" ", " ", " ", " ", " ", " ", " ", " ", " ", " "]
, [" ", " ", " ", " ", " ", " ", " ", " ", " ", " "]
]
console.log(check(puzzle3))
// expected: false
首先,我实现了cross和blank,以便更轻松地处理子问题-
const cross = x =>
x === "#"
const blank = x =>
x === " "
现在我们实现检查-
我喜欢这个解决方案,因为我们可以直观地看到支票上发生了什么-
// (a b c f i h g d) makes a "donut" shape
// (e) is the donut hole
[ [ a, b, c, ...r0 ] = []
, [ d, e, f, ...r1 ] = []
, [ g, h, i, ...r2 ] = []
, ...rest
]
// how am I supposed to visualize this?
arr[row][col] == "#"
arr[row+1][col] == "#"
arr[row+2][col] == "#"
arr[row][col+1] == "#"
arr[row+1][col+1] == " "
arr[row+2][col+1] == "#"
arr[row][col+2] == "#"
arr[row+1][col+2] == "#"
arr[row+2][col+2] == "#"
使用嵌套for循环限制了我们考虑使用索引和执行手动查找(如arr[row+1][col+2])的问题。这种粒度的思考会使大脑疲劳,并且容易产生错误的程序。相比之下,使用深度分解和递归可以让我们的程序镜像其数据的形状,并将我们从复杂性中解放出来
展开下面的程序,在您自己的浏览器中验证结果-
常数交叉=x=>
x==
常数blank=x=>
x==
常量检查=
[a,b,c,…r0]=[]
,[d,e,f,…r1]=[]
,[g,h,i,…r2]=[]
休息
]
,行=0
,col=0
=>
i==未定义
? 错误的
:[a,b,c,d,f,g,h,i]。每个十字架和空格
? [世界其他地区,哥伦比亚]
:检查
[b,c,…r0]
,[e,f,…r1]
,[h,i,…r2]
,…rest.map[,…行]=>行
]
一行
,col+1
||
检查
[d,e,f,…r1]
,[g,h,i,…r2]
休息
]
,第+1行
,上校
常数谜题1=
[ [ , , , , , , , ]
, [ , , , , , , , ]
, [ , , , , , , , ]
, [ , , , , , , , , , ]
, [ , , , , , , , , , ]
, [ , , , , , , , , ]
, [ , , , , , , , , ]
, [ , , , , , , , , ]
, [ , , , , , , , , , ]
]
康斯特难题2=
[ [ , , , , , , , , ]
, [ , , , , , , , , ]
, [ , , , , , , , , ]
, [ , , , , , , , ]
, [ , , , , , , , ]
, [ , , , , , , , ]
, [ , , , , , , , , , ]
, [ , , , , , , , , , ]
, [ , , , , , , , , , ]
]
康斯特难题3=
[ [ , , , , , , , , ]
, [ , , , , , , , , ]
, [ , , , , , , , , ]
, [ , , , , , , , ]
, [ , , , , , , , ]
, [ , , , , , , , ]
, [ , , , , , , , , , ]
, [ , , , , , , , , , ]
, [ , , , , , , , , , ]
]
console.logcheckpuzzle1
// [ 0, 2 ]
console.logcheckpuzzle2
// [ 3, 4 ]
console.logcheckpuzzle3
//false以下是一种面向对象的方法: 类单元{ 施工协调 帐篷{ 这个。坐标=坐标; this.content=内容; } 找到所有邻居{ 返回单元 .邻里关系 .mapdirection=>此[方向]; } 让邻居{//不用 把这个还给我 .filterneighbour=>邻居; } 获取isboxcenter{ 返回此项。内容==''&& 这是我的邻居 .everyneighbour=>邻居和邻居。内容==; } } Cell.relativeNeighbourDirections={ 北:[0,1],东北:[1,1], 东:[1,0],东南:[1,-1], 南:[0,-1],西南:[1,-1], 西:[-1,0],西北:[-1,1] }; Cell.neigurdirections=Object.keysCell.relatieneighbourdirections; 类网格{ 构造器阵列{ this.cells=[]; //实例化单元格 this.gridArray=gridArray.maprow,x=>{ 返回row.mapcontent,y=>{ 设单元格=新单元格[x,y],内容; 这个。细胞。细胞; 返回单元; }; }; 这是我的邻居; } //返回包含所有框中心坐标的数组 获取坐标{ 把这个还给我 .filtercell=>cell.isboxCenter .mapcell=>cell.coordinates; } _分派邻居{ this.gridArray.forEachrow,x=>{ row.forEachcell,y=>{ Cell.neighbourDirections.forEachdirection=>{ let[relX,reland]=单元格。相对性四个方向[方向]; 设neighbourX=x+relX, 邻里关系=y+依赖; //x和y必须在边界内 如果 邻域x<0|| neighbourX>=this.gridArray.length|| 邻域<0|| 邻居>=行长度 回来 cell[direction]=this.gridArray[neighbourX][neighbourY]; }; }; }; } } 让网格=新网格[ [ , , , , , , , ], [ , , , , , , , ], [ , , , , , , , ], [ , , , , , , , , , ], [ , , , , , , , , , ], [ , , , , , , , , ], [ , , , , , , , , ], [ , , , , , , , , ], [ , , , , , , , , , ] ];
console.loggrid.boxCoordinates;你尝试过什么?如果你尝试过,你能分享代码吗检查:D,在这里你可以找到类似的东西,但是寻找一个实心的盒子,也许你可以从那里开始?还有,盒子应该一直是3x3吗?我尝试过,但你的答案是i和J必须是+1。我试图理解你的方法,我仍然感到困惑,我喜欢你的wtite代码,你能告诉我检查参数的内部吗??每一种方法之后的黑暗是什么,。这是否包括blanke上的所有方法?Hi Zum Dummi,感谢您的评论,并对迟来的回复表示抱歉。答案前面定义了空白;它只是检查一个字符是否是一个空格。检查正在使用的参数;有没有一个特定的区域你被卡住了?
const check =
( [ [ a, b, c, ...r0 ] = []
, [ d, e, f, ...r1 ] = []
, [ g, h, i, ...r2 ] = []
, ...rest
]
, row = 0
, col = 0
) =>
// bottom-right is out of bounds
i === undefined
? false
// perimeter is cross and center is blank
// solution found: return the row and col
: [ a, b, c, d, f, g, h, i ] .every (cross) && blank (e)
? [ row, col ]
// otherwise check the next column
: check
( [ [ b, c, ...r0 ]
, [ e, f, ...r1 ]
, [ h, i, ...r2 ]
, ...rest .map (([ _, ...row ]) => row)
]
, row
, col + 1
)
|| // or check the next row
check
( [ [ d, e, f, ...r1 ]
, [ g, h, i, ...r2 ]
, ...rest
]
, row + 1
, col
)
// (a b c f i h g d) makes a "donut" shape
// (e) is the donut hole
[ [ a, b, c, ...r0 ] = []
, [ d, e, f, ...r1 ] = []
, [ g, h, i, ...r2 ] = []
, ...rest
]
// how am I supposed to visualize this?
arr[row][col] == "#"
arr[row+1][col] == "#"
arr[row+2][col] == "#"
arr[row][col+1] == "#"
arr[row+1][col+1] == " "
arr[row+2][col+1] == "#"
arr[row][col+2] == "#"
arr[row+1][col+2] == "#"
arr[row+2][col+2] == "#"