循环通过阵列直到找到解决方案

时间:2016-01-15 20:32:01

标签: javascript arrays

所以我有下面的数组,每个数组的第一个值是道路名称,第二个值是道路的任何连接,第三个值现在可以忽略(所有假道路)。我目前正试图通过查看连接找到从一条路到另一条路的最快路线(最少的步骤)。我的代码目前正在查看我自己定义的阶段,但是当我添加更多连接时我将不得不添加进一步的逻辑 - 无论如何都要循环一个未定义的数量,直到它找到最佳路线(没有点它无法找到路线)?

阵列:

  var road = [
        //Name, connection, [left, middle, right]
            ["Upper-G", ["Lower-G", "Left Corner", "Top Corner"], []],
            ["Upper-TW", ["Lower-TW", "Left Corner", "Right Corner"], []],
            ["Upper-RG", ["Lower-RG", "Upper-RG", "Top Corner", "Right Corner"], []],
            ["Lower-G", ["Upper-G", "Left Corner", "Top Corner"], [] ],
            ["Lower-TW", ["Upper-TW", "Lower-PD", "Upper-PD", "Left Corner", "Right Corner"], []],
            ["Lower-RG", ["Upper-RG", "Top Corner", "Right Corner"], []],
            ["Left Corner", ["Lower-G", "Upper-G", "Lower-TW", "Upper-TW"], []],
            ["Top Corner", ["Upper-G", "Lower-G", "Lower-RG", "Upper-RG"], []],
            ["Right Corner", ["Upper-RG", "Lower-RG", "Upper-TW", "Lower-TW"], []],
            ["Upper-PD", ["Lower-PD", "Lower-TW", "Upper-TW"], []],
            ["Lower-PD", ["Upper-PD", "Lower-TW", "Upper-TW"], []]
        ];

代码: 我打电话给findPath(' roadWeAreAt',' roadToGetTo')来获得结果

function findPath(cRoad, fRoad){
        //Find roads in array
        var cOriginalPos = findRoadInArr(cRoad);
        var fOriginalPos = findRoadInArr(fRoad);

        //If either cannot be found, return undefined
        if(cOriginalPos == undefined || fOriginalPos == undefined){
            return "undefined";
        }

        //Target arrays for ease and add first road to directions
        var cRoadArray = road[cOriginalPos];
        var fRoadArray = road[fOriginalPos];
        var roadDirections = findBestPath(cRoadArray, fRoadArray);

        //found or not
        if(roadDirections != undefined){
            console.log(roadDirections);
        }
        else {

            console.log("not found");
        }

    }

    function findBestPath(cRoadArray, fRoadArray){
        var roadDirections = findInitialPath(cRoadArray, fRoadArray);

        if(roadDirections == undefined){
            roadDirections = findSecondPath(cRoadArray, fRoadArray);

            if(roadDirections == undefined){
                roadDirections = findThirdPath(cRoadArray, fRoadArray);

                if(roadDirections == undefined){
                    roadDirections = findFourthPath(cRoadArray, fRoadArray);
                }
            }
        }
        return roadDirections;
    }

    function findInitialPath(cRoadArray, fRoadArray){
    var roadDirections = [cRoadArray[0]];
        //Loop through current roadarray
        for(var i = 0; i < cRoadArray[1].length; i++){
            //If it's next to each other
            if(cRoadArray[1][i] == fRoadArray[0]){
                roadDirections.push(fRoadArray[0]);
                return roadDirections;
            }
        }
    }


    function findSecondPath(cRoadArray, fRoadArray){
        var roadDirections = [cRoadArray[0]];
        //Loop through current roadarray
        for(var i = 0; i < cRoadArray[1].length; i++){
            //If its in an inbetween road
            for(var j = 0; j < fRoadArray[1].length; j++){
                if(cRoadArray[1][i] == fRoadArray[1][j]){
                    roadDirections.push(cRoadArray[1][i]);
                    roadDirections.push(fRoadArray[0]);
                    return roadDirections;
                }
            }
        }
    }

    function findThirdPath(cRoadArray, fRoadArray){
        var roadDirections = [cRoadArray[0]];

        //Loop through current roadArray
        for(var i = 0; i < cRoadArray[1].length; i++){
            var newcurrentroad = road[findRoadInArr(cRoadArray[1][i])];
            //Loop through 
            var tryDirections = findSecondPath(newcurrentroad, fRoadArray);

            if(tryDirections != undefined){
                //roadDirections.push(cRoadArray[1][i]);
                for(var j = 0; j < tryDirections.length; j++){
                    roadDirections.push(tryDirections[j]);
                }
                return roadDirections;
            }
        }
    }

    function findFourthPath(cRoadArray, fRoadArray){
        var roadDirections = [cRoadArray[0]];

        for(var i = 0; i < cRoadArray[1].length; i++){
            var newcurrentroad = road[findRoadInArr(cRoadArray[1][i])];
            //Loop through 
            var tryDirections = findThirdPath(newcurrentroad, fRoadArray);

            if(tryDirections != undefined){
                for(var j = 0; j < tryDirections.length; j++){
                    roadDirections.push(tryDirections[j]);
                }
                return roadDirections;
            }
        }   
    }


    function findRoadInArr(roadName){
        for(var i = 0; i < road.length; i++){
            if(road[i][0] == roadName){
                return i;
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

感谢大家的帮助,一旦我找到了algorthims,我就遇到了这个完整的解决方案,它将完全满足我的需求: https://github.com/qiao/PathFinding.js/