红线以下的所有值都应为6,通过相邻位置与顶部接触的所有内容都应保持不变。我要求你至少朝哪个方向看,我什至不知道如何实现这一点。
var arrayGrid = [
[5, 2, 3, 0, 2, 0, 3, 5, 3, 5],
[4, 3, 3, 4, 4, 0, 2, 0, 2, 0],
[2, 3, 5, 3, 1, 1, 4, 5, 4, 2],
[2, 1, 3, 0, 1, 1, 2, 2, 1, 5],
[3, 4, 3, 4, 1, 4, 2, 6, 2, 2],
[5, 3, 1, 0, 5, 2, 6, 2, 1, 0],
[0, 0, 6, 6, 3, 6, 6, 6, 6, 1],
[6, 6, 6, 6, 6, 6, 6, 4, 2, 3],
[6, 3, 6, 6, 6, 6, 3, 4, 3, 4],
[6, 6, 6, 6, 6, 6, 6, 5, 2, 6],
[6, 6, 6, 6, 6, 6, 1, 6, 6, 2],
[6, 6, 6, 6, 6, 6, 6, 6, 6, 6],
[6, 6, 6, 6, 6, 6, 6, 6, 6, 6],
[6, 6, 6, 6, 6, 6, 6, 6, 6, 6]
]
var line = []
function findClear(y, x) {
if (x >= 10) {
return line;
}
if (arrayGrid[y - 1][x + 1] == 6) {
line.push(y - 1)
findClear(y - 1, x + 1)
} else if (arrayGrid[y][x + 1] == 6) {
line.push(y)
findClear(y, x + 1)
} else if (arrayGrid[y + 1][x + 1] == 6) {
line.push(y + 1)
findClear(y + 1, x + 1)
}
}
findClear(7, 0)
console.log(line)
if(line.length < 10){
line = [];
findClearOpposite(9, 10)
}
function findClearOpposite(y, x) {
if (x < 0) {
return line;
}
if (arrayGrid[y - 1][x - 1] == 6) {
line.push(y - 1)
findClearOpposite(y - 1, x - 1)
} else if (arrayGrid[y][x - 1] == 6) {
line.push(y)
findClearOpposite(y, x - 1)
} else if (arrayGrid[y + 1][x - 1] == 6) {
line.push(y + 1)
findClearOpposite(y + 1, x - 1)
}
}
console.log(line)
问题的解决方法很简单,如果从左到右不起作用,那么我们从右到左尝试。非常感谢您的想法和建议。