给定矩阵/图形/多维数组的输入(不确定它们在编程中是否具有唯一的定义),下面的函数应该输出最大岛的大小。土地用1表示,水用0表示,两个区域只有在水平或垂直(不是对角线)相邻时才连接。
下面的算法有效,但只有当我保存数组中的所有岛时,才是“islands”变量,然后返回保存的最大值。
最初,我试图仅用“最大”变量跟踪最大值的大小。但是,由于某种原因,它会一直重置为初始值0。
为什么会这样?我特别困惑的是,为什么这可能发生在“最大”变量而不是“孤岛”变量,因为两者似乎具有完全相同的范围。
const bigIsland = matrix => {
let biggest = 0
let islands = []
let visited = matrix.map(row => row.map(node => false))
for(let y=0; y<matrix.length; y++){
for(let x=0; x<matrix[y].length; x++){
if (visited[y][x]) continue
checkIsland(matrix, visited, y, x, islands, biggest)
}
}
return Math.max(...islands)
}
const checkIsland = (matrix, visited, y, x, islands, biggest) => {
console.log("biggestStart", biggest)
console.log("islandsStart", islands)
let currIsland = 0
let nodesToCheck = [[y,x]]
while(nodesToCheck.length){
const [y, x] = nodesToCheck.pop()
if (visited[y][x]) continue
visited[y][x] = true
if (matrix[y][x]===0) continue
currIsland++
console.log("currIsland", currIsland)
getValidNeighbors(matrix, visited, y, x, nodesToCheck)
}
if (currIsland > biggest) biggest = currIsland
if (currIsland > 0) islands.push(currIsland)
console.log("biggest", biggest)
console.log("islands", islands)
}
const getValidNeighbors = (matrix, visited, y, x, nodesToCheck) => {
if(y>0) nodesToCheck.push([y-1, x])
if(y<matrix.length-1) nodesToCheck.push([y+1, x])
if(x>0) nodesToCheck.push([y, x-1])
if(x<matrix[y].length-1) nodesToCheck.push([y, x+1])
}
bigIsland([ [1, 0, 1],
[1, 0, 1],
[1, 0, 0] ])
将原始变量传递给函数(例如biggest
到checkIsland
)时,该函数中的任何内容都不能更改调用函数中对该变量的原始引用。为了改变biggest
中的bigIsland
,你需要明确地从biggest
返回找到的checkIsland
:
const bigIsland = matrix => {
let biggest = 0
let islands = []
let visited = matrix.map(row => row.map(node => false))
for(let y=0; y<matrix.length; y++){
for(let x=0; x<matrix[y].length; x++){
if (visited[y][x]) continue
biggest = checkIsland(matrix, visited, y, x, islands, biggest)
}
}
return Math.max(...islands)
}
const checkIsland = (matrix, visited, y, x, islands, biggest) => {
console.log("biggestStart", biggest)
console.log("islandsStart", islands)
let currIsland = 0
let nodesToCheck = [[y,x]]
while(nodesToCheck.length){
const [y, x] = nodesToCheck.pop()
if (visited[y][x]) continue
visited[y][x] = true
if (matrix[y][x]===0) continue
currIsland++
console.log("currIsland", currIsland)
getValidNeighbors(matrix, visited, y, x, nodesToCheck)
}
if (currIsland > biggest) biggest = currIsland
if (currIsland > 0) islands.push(currIsland)
console.log("biggest", biggest)
console.log("islands", islands)
return biggest;
}
const getValidNeighbors = (matrix, visited, y, x, nodesToCheck) => {
if(y>0) nodesToCheck.push([y-1, x])
if(y<matrix.length-1) nodesToCheck.push([y+1, x])
if(x>0) nodesToCheck.push([y, x-1])
if(x<matrix[y].length-1) nodesToCheck.push([y, x+1])
}
bigIsland([ [1, 0, 1],
[1, 0, 1],
[1, 0, 0] ])
另一方面,当你传递islands
时,islands
是一个数组,它是一个对象(非原始的),所以当islands
在checkIsland
内变异时,它的变化也会在其他地方都可见。引用非基元的变量本质上是对内存中对象的内存位置的引用 - 传递时不会复制对象,除非您明确地这样做。
函数biggest
中的变量bigIsland
独立于biggest
中的变量checkIsland
。在一个函数中更改变量对另一个函数没有影响。数组是不同的,因为当您传递数组时,您将获得对变量中相同数组的引用。考虑:
function one() {
let variable = 0;
console.log("variable in on before calling two ", variable)
two(variable)
console.log("variable in on after calling two ", variable)
}
function two(variable) {
variable += 200
console.log("variable in two", variable)
}
one()
如果您希望两个函数都改变同一个变量,您可以将变量放在一个范围内,两者都可以看到:
let variable = 0
function one(){
console.log("variable in on before calling two ", variable)
two()
console.log("variable in on after calling two ", variable)
}
function two() {
variable += 200
console.log("variable in two", variable)
}
one()
或者你可以将它作为一个对象传递,这是一种非常标准的传递方式:
function one() {
let obj = {variable: 0}
console.log("variable in on before calling two ", obj.variable)
two(obj)
console.log("variable in on after calling two ", obj.variable)
}
function two(obj) {
// notice we are not reassigning the variable obj with obj = 200
// but changing the thing obj refers to
obj.variable += 200
console.log("variable in two", obj.variable)
}
one()