簡體   English   中英

JS 最大調用棧超過 P5

[英]JS maximum call stack exceeded P5

我正在嘗試編寫程序來計算 p5.js 中最大的顏色連接塊。 問題是,當我遞歸調用框鄰居時,它給出了“最大調用堆棧超出”,我知道錯誤的含義,但我不知道它為什么會發生。 這是我的代碼:

 class Box { constructor(id, x, y, w, h, col, colId) { this.id = id; this.x = x; this.y = y; this.w = w; this.h = h; this.col = col; this.colId = colId; } render() { fill(this.col); stroke(1); rect(this.x * this.w, this.y * this.w, this.w, this.h); } getNeighbour(visited, x, y) { if(x - 1 < 0 || x + 1 >= rows || y - 1 < 0 || y + 1 >= cols) { return -1; } else { this.col = color(10,10,10); arr[x-1][y].getNeighbour(visited,x-1,y); arr[x+1][y].getNeighbour(visited,x+1,y); arr[x][y-1].getNeighbour(visited,x,y-1); arr[x][y+1].getNeighbour(visited,x,y+1); } if(!this.contains(this.id)) { visited.push(this.id); } } contains() { for(let i = 0; i < visited.length; i++) { if(this.id == visited[i]) return true; } return false; } }

請告訴我問題出在哪里。 我當時也嘗試做檢查。 首先是左邊的鄰居,這就像魅力一樣。 但是當我也添加了正確的一個時,就會出現這個錯誤。 我認為我有適當的默認情況可以滿足,但現實可能有所不同。

當你從一個盒子開始時,它會遞歸到它左邊、右邊、上面和下面的盒子。 這四個盒子也是如此。 但是,如果您在上面的框上並且它調用下面的框,那么您又回到了起點。 如此反復,直到您的堆棧用完為止。

根據您要執行的操作,一個簡單的解決方案是拉取“包含”檢查,如果之前訪問過該框,則中止:

getNeighbour(visited, x, y) {
    if( isOutsideBounds(x,y) || visited.indexOf(this.id) > -1) {
      return -1; 
    }
    this.col = color(10,10,10);
    visited.push(this.id)
    arr[x-1][y].getNeighbour(visited,x-1,y);
    arr[x+1][y].getNeighbour(visited,x+1,y);
    arr[x][y-1].getNeighbour(visited,x,y-1);
    arr[x][y+1].getNeighbour(visited,x,y+1); 
}  

此外,您的contains()函數似乎訪問了不在函數范圍內的visited數組。

代替:

else {
  this.col = color(10,10,10);
  arr[x-1][y].getNeighbour(visited,x-1,y);
  arr[x+1][y].getNeighbour(visited,x+1,y);
  arr[x][y-1].getNeighbour(visited,x,y-1);
  arr[x][y+1].getNeighbour(visited,x,y+1);
}
if(!this.contains(this.id)) {
  visited.push(this.id); 
}

嘗試:

else if(!this.contains(this.id)) {
  visited.push(this.id);
  this.col = color(10,10,10);
  arr[x-1][y].getNeighbour(visited,x-1,y);
  arr[x+1][y].getNeighbour(visited,x+1,y);
  arr[x][y-1].getNeighbour(visited,x,y-1);
  arr[x][y+1].getNeighbour(visited,x,y+1);
}

進一步:我不確定this.contains是否正確,也許嘗試: visited.contains...

這是一種抽象的方法。 您可以拿一個島嶼計數器並計算每個島嶼的物品。

 function check(array) { function test(array, i, j, value) { if (array[i] && array[i][j] === -1) { count[value] = (count[value] || 0) + 1; array[i][j] = value; test(array, i - 1, j, value); test(array, i + 1, j, value); test(array, i, j - 1, value); test(array, i, j + 1, value); return true; } } var value = 1, count = {}; array.forEach(a=> a.forEach((b, i, bb) => bb[i] = -b)); array.forEach((a, i, aa) => a.forEach((b, j) => test(aa, i, j, value) && value++)); array.map(a => console.log(...a)); return count; } console.log(check([[1, 0, 1], [1, 0, 0], [1, 1, 1]])); console.log(check([[1, 0, 1], [0, 1, 0], [1, 0, 1]])); console.log(check([[1, 0, 0, 1, 1], [1, 1, 1, 0, 0], [0, 0, 1, 0, 1], [0, 0, 1, 0, 1], [1, 1, 1, 0, 1]]));
 .as-console-wrapper { max-height: 100% !important; top: 0; }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM