广度优先搜索Javascript(算法或输入)

广度优先搜索Javascript(算法或输入),javascript,breadth-first-search,Javascript,Breadth First Search,这应该是一个广度优先的搜索,在搜索过程中给父母贴上标签。当它完成时,它会将这些父母追溯到起点,以创建一条路径 地图只是这些瓷砖的一个数组 function Tile(x, y){ this.x = x; this.y = y; //for BFS this.px = 0; this.py = 0; this.visited = false; this.is = " "; this.n = null; this.e =

这应该是一个广度优先的搜索,在搜索过程中给父母贴上标签。当它完成时,它会将这些父母追溯到起点,以创建一条路径

地图只是这些瓷砖的一个数组

function Tile(x, y){

    this.x = x;
    this.y = y;

    //for BFS
    this.px = 0;
    this.py = 0;
    this.visited = false;

    this.is = " ";

    this.n = null;
    this.e = null;
    this.s = null;
    this.w = null;
}
我一辈子都搞不清楚这是我的算法还是我的起始数据

如果有人能帮我解决这个问题或将其清除为正确,我将非常感激

    function FakeBFS(){
    var me = this;

    this.get_path = function(sx, sy, ex, ey, map){
        var x = sx;
        var y = sy;
        var unfinished = [{"x":sx, "y":sy}];

        //walk until we can't or we reach our destination
        while(unfinished.length && x != ex && y != ey){

            var new_location = unfinished.shift();

            x = new_location.x;
            y = new_location.y;

            //if we haven't been here...
            if(map.tiles[x][y].visited == false){

                console.log("\n\nlooking for path from:"+x+","+y+" to:"+ex+","+ey);
                var nesw_array = new Array(
                                            [x,y+1], //n
                                            [x+1,y], //e
                                            [x,y-1], //s
                                            [x-1,y]);//w
                                            console.log(nesw_array);

                //add all the vertices we care about to unfinished
                for(var nesw=0; nesw<4; nesw++){
                    var vx = nesw_array[nesw][0];
                    var vy = nesw_array[nesw][1];
                    console.log(nesw_array[nesw]);
                    if( (map.tiles[vx][vy].is == " "
                    ||  map.tiles[vx][vy].is == "H")
                    &&  map.tiles[vx][vy].visited == false){
                        console.log("parenting["+nesw+"]"+map.tiles[vx][vy].x+","+map.tiles[vx][vy].y+" to:"+x+","+y);
                        //set the route we took to get here
                        map.tiles[vx][vy].px = x;
                        map.tiles[vx][vy].py = y;
                        unfinished.push({"x":map.tiles[vx][vy].x, "y":map.tiles[vx][vy].y});
                    }
                }
                //visit this vertex
                map.tiles[x][y].visited = true;
            }

            console.log("END LOOP looking for path from:"+x+","+y+" to:"+ex+","+ey);
        }

        //walk back to start for our path
        var path = Array();
        if(x == ex && y == ey){
            while(x != sx && y != sy){
                path.push({"x":x,"y":y});
                console.log(x+","+y);
                x = map.tiles[x][y].px;
                y = map.tiles[x][y].py;
            }
            path.push({"x":x,"y":y});
            console.log("Path:"+util.inspect(path));
            return path;
        }
        return false;
    }
}
这是我的调试输出

C:\Users\trans\Documents\JS>node test_bfs.js


looking for path from:3,3 to:5,2
[ [ 3, 4 ], [ 4, 3 ], [ 3, 2 ], [ 2, 3 ] ]
[ 3, 4 ]
parenting[0]3,4 to:3,3
[ 4, 3 ]
parenting[1]4,3 to:3,3
[ 3, 2 ]
[ 2, 3 ]
END LOOP looking for path from:3,3 to:5,2


looking for path from:3,4 to:5,2
[ [ 3, 5 ], [ 4, 4 ], [ 3, 3 ], [ 2, 4 ] ]
[ 3, 5 ]
parenting[0]3,5 to:3,4
[ 4, 4 ]
parenting[1]4,4 to:3,4
[ 3, 3 ]
[ 2, 4 ]
parenting[3]2,4 to:3,4
END LOOP looking for path from:3,4 to:5,2


looking for path from:4,3 to:5,2
[ [ 4, 4 ], [ 5, 3 ], [ 4, 2 ], [ 3, 3 ] ]
[ 4, 4 ]
parenting[0]4,4 to:4,3
[ 5, 3 ]
[ 4, 2 ]
parenting[2]4,2 to:4,3
[ 3, 3 ]
END LOOP looking for path from:4,3 to:5,2


looking for path from:3,5 to:5,2
[ [ 3, 6 ], [ 4, 5 ], [ 3, 4 ], [ 2, 5 ] ]
[ 3, 6 ]
parenting[0]3,6 to:3,5
[ 4, 5 ]
parenting[1]4,5 to:3,5
[ 3, 4 ]
[ 2, 5 ]
parenting[3]2,5 to:3,5
END LOOP looking for path from:3,5 to:5,2


looking for path from:4,4 to:5,2
[ [ 4, 5 ], [ 5, 4 ], [ 4, 3 ], [ 3, 4 ] ]
[ 4, 5 ]
parenting[0]4,5 to:4,4
[ 5, 4 ]
parenting[1]5,4 to:4,4
[ 4, 3 ]
[ 3, 4 ]
END LOOP looking for path from:4,4 to:5,2


looking for path from:2,4 to:5,2
[ [ 2, 5 ], [ 3, 4 ], [ 2, 3 ], [ 1, 4 ] ]
[ 2, 5 ]
parenting[0]2,5 to:2,4
[ 3, 4 ]
[ 2, 3 ]
[ 1, 4 ]
parenting[3]1,4 to:2,4
END LOOP looking for path from:2,4 to:5,2
END LOOP looking for path from:4,4 to:5,2


looking for path from:4,2 to:5,2
[ [ 4, 3 ], [ 5, 2 ], [ 4, 1 ], [ 3, 2 ] ]
[ 4, 3 ]
[ 5, 2 ]
parenting[1]5,2 to:4,2
[ 4, 1 ]
parenting[2]4,1 to:4,2
[ 3, 2 ]
END LOOP looking for path from:4,2 to:5,2
##########
#        #
#        #
#        #
#        #
#        #
# .H ..  #
# .. H.  #
#        #
##########

[ false ]
[0]undefined,undefined
C:\Users\trans\Documents\JS\testmap.js:102
                        me.tiles[thispath.x][thispath.y].is = "+";
                                            ^

TypeError: Cannot read property 'undefined' of undefined
    at TestMap.mark_paths (C:\Users\trans\Documents\JS\testmap.js:102:24)
    at new TestMap (C:\Users\trans\Documents\JS\testmap.js:110:7)
    at Object.<anonymous> (C:\Users\trans\Documents\JS\test_bfs.js:2:11)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.runMain (module.js:604:10)
    at run (bootstrap_node.js:390:7)
C:\Users\trans\Documents\JS>节点测试
正在查找从:3,3到:5,2的路径
[ [ 3, 4 ], [ 4, 3 ], [ 3, 2 ], [ 2, 3 ] ]
[ 3, 4 ]
养育[0]3,4至:3,3
[ 4, 3 ]
为人父母[1]4,3至:3,3
[ 3, 2 ]
[ 2, 3 ]
结束循环,查找从:3,3到:5,2的路径
正在查找从:3,4到:5,2的路径
[ [ 3, 5 ], [ 4, 4 ], [ 3, 3 ], [ 2, 4 ] ]
[ 3, 5 ]
养育[0]3,5至:3,4
[ 4, 4 ]
为人父母[1]4,4至:3,4
[ 3, 3 ]
[ 2, 4 ]
为人父母[3]2,4至:3,4
结束循环,查找从:3,4到:5,2的路径
正在查找从:4,3到:5,2的路径
[ [ 4, 4 ], [ 5, 3 ], [ 4, 2 ], [ 3, 3 ] ]
[ 4, 4 ]
养育[0]4,4至:4,3
[ 5, 3 ]
[ 4, 2 ]
为人父母[2]4,2至:4,3
[ 3, 3 ]
结束循环,查找从:4,3到:5,2的路径
正在查找从:3,5到:5,2的路径
[ [ 3, 6 ], [ 4, 5 ], [ 3, 4 ], [ 2, 5 ] ]
[ 3, 6 ]
养育[0]3,6至:3,5
[ 4, 5 ]
为人父母[1]4,5至:3,5
[ 3, 4 ]
[ 2, 5 ]
为人父母[3]2,5至:3,5
结束循环,查找从:3,5到:5,2的路径
正在查找从:4,4到:5,2的路径
[ [ 4, 5 ], [ 5, 4 ], [ 4, 3 ], [ 3, 4 ] ]
[ 4, 5 ]
养育[0]4,5至:4,4
[ 5, 4 ]
为人父母[1]5,4至:4,4
[ 4, 3 ]
[ 3, 4 ]
结束循环,查找从:4,4到:5,2的路径
正在查找从:2,4到:5,2的路径
[ [ 2, 5 ], [ 3, 4 ], [ 2, 3 ], [ 1, 4 ] ]
[ 2, 5 ]
为人父母[0]2,5至:2,4
[ 3, 4 ]
[ 2, 3 ]
[ 1, 4 ]
为人父母[3]1,4至:2,4
结束循环,查找从:2,4到:5,2的路径
结束循环,查找从:4,4到:5,2的路径
正在查找从:4,2到:5,2的路径
[ [ 4, 3 ], [ 5, 2 ], [ 4, 1 ], [ 3, 2 ] ]
[ 4, 3 ]
[ 5, 2 ]
为人父母[1]5,2至:4,2
[ 4, 1 ]
为人父母[2]4,1至:4,2
[ 3, 2 ]
结束循环,查找从:4,2到:5,2的路径
##########
#        #
#        #
#        #
#        #
#        #
#.H#
# .. H#
#        #
##########
[错误]
[0]未定义,未定义
C:\Users\trans\Documents\JS\testmap.JS:102
me.tiles[thispath.x][thispath.y].is=“+”;
^
TypeError:无法读取undefined的属性“undefined”
在TestMap.mark_路径(C:\Users\trans\Documents\JS\TestMap.JS:102:24)
在新的TestMap(C:\Users\trans\Documents\JS\TestMap.JS:110:7)
反对。(C:\Users\trans\Documents\JS\test\u bfs.JS:2:11)
在模块处编译(Module.js:570:32)
在Object.Module.\u extensions..js(Module.js:579:10)
在Module.load(Module.js:487:32)
在tryModuleLoad时(module.js:446:12)
在Function.Module.\u加载(Module.js:438:3)
位于Module.runMain(Module.js:604:10)
运行时(bootstrap_node.js:390:7)

主要问题在于我离开循环的条件,以及在获得数据之前修改引用变量的条件

           .......................

    //walk until we can't or we reach our destination
    while(unfinished.length){

        var new_location = unfinished.shift();

        x = new_location.x;
        y = new_location.y;

        if(x == ex && y == ey){
            unfinished.length = 0;
            continue;
        }

    ..........................

    //walk back to start for our path
    var path = Array();
    if(x == ex && y == ey){
        console.log("here");
        while(!(x == sx && y == sy)){
            path.push({"x":x,"y":y});
            console.log(x+","+y);
            //temporary to not screw up reference
            var tempx = map.tiles[x][y].px;
            tempx = map.tiles[x][y].px;
            y = map.tiles[x][y].py;
            x = tempx;
            console.log(x+","+y);
        }
        path.push({"x":x,"y":y});
        return path;
    }
    return false;

也许您还可以添加一些数据以使部件正常工作。此行中的me对象是什么:for(var i=0;i“me”是一个通用占位符,当“this”更改范围时,我在对象内部使用它来保留对父对象的引用。我只是专门使用它来避免意外引用错误的“this”。它是Map()对象。我已经将所有代码更新为我现在使用的代码…我删除了尽可能多的对象引用,移动到原始数据值进行评估…我仍然无法破解它。
C:\Users\trans\Documents\JS>node test_bfs.js


looking for path from:3,3 to:5,2
[ [ 3, 4 ], [ 4, 3 ], [ 3, 2 ], [ 2, 3 ] ]
[ 3, 4 ]
parenting[0]3,4 to:3,3
[ 4, 3 ]
parenting[1]4,3 to:3,3
[ 3, 2 ]
[ 2, 3 ]
END LOOP looking for path from:3,3 to:5,2


looking for path from:3,4 to:5,2
[ [ 3, 5 ], [ 4, 4 ], [ 3, 3 ], [ 2, 4 ] ]
[ 3, 5 ]
parenting[0]3,5 to:3,4
[ 4, 4 ]
parenting[1]4,4 to:3,4
[ 3, 3 ]
[ 2, 4 ]
parenting[3]2,4 to:3,4
END LOOP looking for path from:3,4 to:5,2


looking for path from:4,3 to:5,2
[ [ 4, 4 ], [ 5, 3 ], [ 4, 2 ], [ 3, 3 ] ]
[ 4, 4 ]
parenting[0]4,4 to:4,3
[ 5, 3 ]
[ 4, 2 ]
parenting[2]4,2 to:4,3
[ 3, 3 ]
END LOOP looking for path from:4,3 to:5,2


looking for path from:3,5 to:5,2
[ [ 3, 6 ], [ 4, 5 ], [ 3, 4 ], [ 2, 5 ] ]
[ 3, 6 ]
parenting[0]3,6 to:3,5
[ 4, 5 ]
parenting[1]4,5 to:3,5
[ 3, 4 ]
[ 2, 5 ]
parenting[3]2,5 to:3,5
END LOOP looking for path from:3,5 to:5,2


looking for path from:4,4 to:5,2
[ [ 4, 5 ], [ 5, 4 ], [ 4, 3 ], [ 3, 4 ] ]
[ 4, 5 ]
parenting[0]4,5 to:4,4
[ 5, 4 ]
parenting[1]5,4 to:4,4
[ 4, 3 ]
[ 3, 4 ]
END LOOP looking for path from:4,4 to:5,2


looking for path from:2,4 to:5,2
[ [ 2, 5 ], [ 3, 4 ], [ 2, 3 ], [ 1, 4 ] ]
[ 2, 5 ]
parenting[0]2,5 to:2,4
[ 3, 4 ]
[ 2, 3 ]
[ 1, 4 ]
parenting[3]1,4 to:2,4
END LOOP looking for path from:2,4 to:5,2
END LOOP looking for path from:4,4 to:5,2


looking for path from:4,2 to:5,2
[ [ 4, 3 ], [ 5, 2 ], [ 4, 1 ], [ 3, 2 ] ]
[ 4, 3 ]
[ 5, 2 ]
parenting[1]5,2 to:4,2
[ 4, 1 ]
parenting[2]4,1 to:4,2
[ 3, 2 ]
END LOOP looking for path from:4,2 to:5,2
##########
#        #
#        #
#        #
#        #
#        #
# .H ..  #
# .. H.  #
#        #
##########

[ false ]
[0]undefined,undefined
C:\Users\trans\Documents\JS\testmap.js:102
                        me.tiles[thispath.x][thispath.y].is = "+";
                                            ^

TypeError: Cannot read property 'undefined' of undefined
    at TestMap.mark_paths (C:\Users\trans\Documents\JS\testmap.js:102:24)
    at new TestMap (C:\Users\trans\Documents\JS\testmap.js:110:7)
    at Object.<anonymous> (C:\Users\trans\Documents\JS\test_bfs.js:2:11)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.runMain (module.js:604:10)
    at run (bootstrap_node.js:390:7)
           .......................

    //walk until we can't or we reach our destination
    while(unfinished.length){

        var new_location = unfinished.shift();

        x = new_location.x;
        y = new_location.y;

        if(x == ex && y == ey){
            unfinished.length = 0;
            continue;
        }

    ..........................

    //walk back to start for our path
    var path = Array();
    if(x == ex && y == ey){
        console.log("here");
        while(!(x == sx && y == sy)){
            path.push({"x":x,"y":y});
            console.log(x+","+y);
            //temporary to not screw up reference
            var tempx = map.tiles[x][y].px;
            tempx = map.tiles[x][y].px;
            y = map.tiles[x][y].py;
            x = tempx;
            console.log(x+","+y);
        }
        path.push({"x":x,"y":y});
        return path;
    }
    return false;