簡體   English   中英

我的迷宮代碼算法無法解決-javascript

[英]My Maze code algorithm not solve - javascript

我是新開發者

我的代碼有問題,不知道是什么問題

我的迷宮代碼有效,但無法解決迷宮問題

有人可以幫助我了解問題什么,或者有其他解決方法?

  var myMaze = [ [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 1, 1, 0, 0, 0, 0, 0], [1, 1, 1, 0, 1, 0, 0, 1, 1, 1], [0, 1, 0, 0, 1, 0, 0, 1, 0, 0], [0, 1, 1, 1, 1, 0, 1, 1, 1, 0], [0, 1, 0, 0, 1, 0, 1, 0, 1, 0], [0, 0, 0, 0, 1, 1, 1, 0, 1, 0], [0, 0, 0, 0, 0, 0, 0, 0, 1, 2], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] ]; function maze(myMaze){ this.find = function(col,row){ console.log(row,col,myMaze[row][col]) if(myMaze[row][col] == 2){ console.log('done') } if(myMaze[row][col] == 1){ console.log('we on the right way') if(row < myMaze.length - 1){ this.find(col,row+1) } if(col< myMaze[row].length -1){ this.find(col+1,row) } if(row > 0){ this.find(col,row-1) } if(col > 0){ this.find(col-1,row) } } } } var maze= new maze(myMaze) maze.find(0,3) 

主要問題是您的算法運行成圓形:從單元格B將訪問它之前訪問過的單元格A,然后從該單元格進行更深的遞歸以再次到達單元格B,並且這種情況將永遠持續下去。 最終,調用堆棧將耗盡內存。

這可以通過跟蹤已訪問的單元格來解決,因此不會再次訪問它們。 盡管可以使用數組完成此操作,但使用Set可以更有效。

其次,為maze構造函數和maze實例使用相同的名稱。 這將使得不可能連續解決兩個迷宮。 取而代之的是,使用慣例將構造函數命名Maze字母大寫: Maze

另外,如果僅輸出算法已找到目標單元的事實,則您沒有有關所找到路徑的任何信息。 最好返回路徑,並讓呼叫者按他們的意願進行處理。

最后,沒有理由在實例上創建find方法:更好的做法是在原型上定義它,以便即使創建多個迷宮實例也只需創建一次。

我還建議放棄老式的構造函數,而使用ES6 class語法,這種語法已經存在了幾年。

這是編碼方法:

 class Maze { constructor(maze) { this.maze = maze; this.width = maze[0].length; this.height = maze.length; } // Added optional argument to indicate cells that should not be visited find(col, row, visited = new Set) { // Create a unique reference for the current cell const cellId = row * this.width + col; // Check that this cell lies within the grid, has a non-zero value, // and has not been visited before if (!this.maze[row] || !this.maze[row][col] || visited.has(cellId)) { return; // No success } visited.add(cellId); // Mark this cell as visited, so it is not visited a second time. //console.log("visiting: ", col, row); // Uncomment to see progress if (this.maze[row][col] == 2) { // Bingo! return [[col, row]]; // Return the path that will be extended during backtracking } // Loop through the 4 directions for (const [addcol, addrow] of [[0, 1],[1, 0],[0, -1],[-1, 0]]) { const found = this.find(col+addcol, row+addrow, visited); // If found, prepend current cell to partial solution and get out of recursion if (found) return [[col, row], ...found]; } } } const myMaze = [ [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 1, 1, 0, 0, 0, 0, 0], [1, 1, 1, 0, 1, 0, 0, 1, 1, 1], [0, 1, 0, 0, 1, 0, 0, 1, 0, 0], [0, 1, 1, 1, 1, 0, 1, 1, 1, 0], [0, 1, 0, 0, 1, 0, 1, 0, 1, 0], [0, 0, 0, 0, 1, 1, 1, 0, 1, 0], [0, 0, 0, 0, 0, 0, 0, 0, 1, 2], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] ]; const maze = new Maze(myMaze); const path = maze.find(0,3); console.log(JSON.stringify(path)); 

我保留一個數組來跟蹤已被訪問的索引。 可以編輯以查看要觀看的路徑

 var myMaze = [
          [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
          [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
          [0, 0, 1, 1, 1, 0, 0, 0, 0, 0],
          [1, 1, 1, 0, 1, 0, 0, 1, 1, 1],
          [0, 1, 0, 0, 1, 0, 0, 1, 0, 0],
          [0, 1, 1, 1, 1, 0, 1, 1, 1, 0],
          [0, 1, 0, 0, 1, 0, 1, 0, 1, 0],
          [0, 0, 0, 0, 1, 1, 1, 0, 1, 0],
          [0, 0, 0, 0, 0, 0, 0, 0, 1, 2],
          [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
        ];

 var c_path=new Array();
 var path=new Array();
 var last ;
 function maze(myMaze){

       this.find = function(col,row){
       if(myMaze[row][col] == 1 ){
            path.push(new Array(row,col));
            console.log(row,col,myMaze[row][col])
       }
       if(myMaze[row][col] == 2){
            last =new Array(row,col);
            console.log('done')
       }
       if(myMaze[row][col] == 1 ){
            if(c_path.includes(row+"-"+col)){
                    return;
            }
            c_path.push(row+"-"+col);
            if(row < myMaze.length - 1){
                    this.find(col,row+1)
             }
             if(col< myMaze[row].length -1){
                this.find(col+1,row)
             }
             if(row > 0){
                this.find(col,row-1)
             }
             if(col > 0){
                this.find(col-1,row)
             }
        }
    }
    this.show =function(){

        for (var i = path.length-1 ;i>0 ;i--){
            var tmp =path[i];
            if((tmp[0] ==last[0] && Math.abs(tmp[1] - last[1]) ==1) ||  (tmp[1] ==last[1] && Math.abs(tmp[0] - last[0]) ==1)){

                last =tmp;
                myMaze[tmp[0]][tmp[1]] =3;
            }
        }
        console.log(myMaze);
    }
}

var maze= new maze(myMaze)
maze.find(0,3)
maze.show();

暫無
暫無

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

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