所有可能的行动采用5x5格? [英] All possible moves in a 5x5 grid?

查看:206
本文介绍了所有可能的行动采用5x5格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 取值○○○○
○○○○○
○○○○○
○○○○○
○○○○Ë
 

我怎么能计算所有可能的路径,不使用同一个广场的两倍,一个人可能需要从取值电子

我已经创建了一个网格阵列[1,1] ... [5,5],但我不知道这是否会工作。

我也映射可能的广场,并试图创建一个记录,并检查和大量的垃圾。

任何标准的公式我可以把在这里使用?

解决方案

有,你可以使用这个好几个标准的路径寻找algorhythms。

这是不相关的JavaScript。

您可以使用一个algorhythm没有启发式,你不应该在第一个解决方案停止。

下面是你如何能做到这一点:

诀窍就是你需要存储已经访问过的广场列表,并检查是否要在每一步重新审视其中的一个。

另一个技巧是您需要的相邻方格之间一定的顺序。 (就像上/右/下/左,这是一个非常愚蠢的algorhythm但罚款这种特殊情况下。)

另外需要能够识别的平方(有可能通过其位置)<​​/ P>

考虑一个递归函数(例如命名为访问):

 函数访问(方){

    方添加到pathlist // pathlist是不路径列表但是正方形的列表这是当前路径

    如果(方是目标){
        添加pathlist的副本到goalslist
    }
    其他 {
        对(在square.adjacencies每个邻接){//这可以通过增加+1和-1的坐标,并检查是否它的溢出计算(小于1 /五个以上)
            如果(邻接在pathlist){
                //什么都不做,我们已经在这里
            }
            其他 {
                访问(相邻)
            }
        }
    }

    从pathlist删除正方形!
}
 

启动此algorythm通过访问(开始)。你得到的是pathlists名单有望在goallist你的结果。

此外,它是只有一半的JavaScript半伪code,但它很容易从它写javascript。

编辑:享受的解决方案:

 &LT;脚本类型=文/ JavaScript的&GT;
VAR开始= [1,1],
    目标= [5,5],
    pathList = [],
    solutionList = [],
    solutionCount = 0,
    宽度= 5,
    高度= 5;


功能squareInArray(方形,数组){
    变种I = 0,
        X =平方[0],
        Y =方[1];

    对于(i = 0; I&LT; array.length;我++){
        如果(X ==阵列[I] [0]放大器;&安培; Y ==阵[I] [1]){
            返回true;
        }
    }
    返回false;
}


功能访问(方){
    变种I = 0,
        X =平方[0],
        Y =方[1],
        邻接= [[X-1,Y],[X + 1,Y],[X,Y + 1],[X,Y-1]];

    pathList.push(广场);

    如果(X ==目标[0]&安培;&安培; Y ==目标[1]){
        变种溶液= pathList.slice(0); //复制绝招
        solutionList.push(溶液);
        solutionCount ++;
        //警报(解决方案);
    }
    其他 {
        对于(i = 0; I&LT; adjacencies.length;我++){
            如果(邻接[I] [0]&小于1 ||邻接[I] [0]≥宽度||邻接[Ⅰ] [1]; 1 ||邻接[Ⅰ] [1]≥高度){
                //溢出
            }
            其他 {
                如果(squareInArray(邻接[I],pathList)){
                    //什么都不做,我们已经在这里
                }
                其他 {
                    访问(邻接[I]);
                }
            }
        }
    }

    pathList.pop();
}

参观(开始);

警报(solutionCount);
&LT; / SCRIPT&GT;
 

8512的目标。也有人要检查我的code是正确的。

看看这个小提琴: http://jsfiddle.net/SoonDead/rd2GN/3/

s o o o o
o o o o o
o o o o o
o o o o o
o o o o e

How can I calculate all possible paths, without using the same square twice, that a person could take to get from s to e?

I've created a grid array [[1,1]...[5,5]] but i dont know if that will work.

I've also mapped possible squares, and tried to create a record and check and lots of junk.

Any standard formula I could put to use here?

解决方案

There are quite a few standard path finding algorhythms you could use for this.

This is not related to javascript.

You can use one algorhythm without heuristics, and you should not stop on the first solution.

Here is how you can do it:

The trick is you'll need to store the already visited squares in a list and check if you are revisiting one of them on every step.

The other trick is you need definite order between the adjacent squares. (Like top/right/bottom/left. Which is a really silly algorhythm but fine for this particular case.)

Also you need to be able to identify the squares (it is possible by its position)

Consider a recursive function (for example name it Visit):

function visit(square) {

    add the square to the pathlist //pathlist is not a list of paths but a list of squares which is the current path

    if (square is the goal) {
        add a copy of the pathlist to the goalslist
    }
    else {
        for (each adjacency in square.adjacencies) { // this can be calculated by adding +1 and -1 to the coordinates, and checking if its overflowing (less then one/more than five)
            if (adjacency is in pathlist) {
                //do nothing we have already been here
            }
            else {
                visit(adjacency)
            }
        }
    }

    remove square from the pathlist!!
}

Start this algorythm by visit(start). You get your result in the goallist which is a list of pathlists hopefully.

Also it's only half javascript-half pseudocode but it is easy to write javascript from it.

EDIT: Enjoy the solution:

<script type="text/javascript">
var start = [1,1],
    goal = [5,5],
    pathList = [],
    solutionList = [],
    solutionCount = 0,
    width = 5,
    height = 5;


function squareInArray(square, array) {
    var i = 0,
        x = square[0], 
        y = square[1];

    for (i = 0; i < array.length; i++) {
        if (x == array[i][0] && y == array[i][1]) {
            return true;
        }
    }
    return false;
}


function visit(square) {
    var i = 0,
        x = square[0], 
        y = square[1],
        adjacencies = [[x-1,y],[x+1,y],[x,y+1],[x,y-1]];

    pathList.push(square);

    if (x == goal[0] && y == goal[1]) {
        var solution = pathList.slice(0); //copy trick
        solutionList.push(solution);
        solutionCount++;
        //alert(solution);
    }
    else {
        for (i = 0; i < adjacencies.length; i++) {
            if (adjacencies[i][0] < 1 || adjacencies[i][0] > width || adjacencies[i][1] < 1 ||adjacencies[i][1] > height) {
                //overflow
            }
            else {
                if (squareInArray(adjacencies[i], pathList)) {
                    //do nothing we have already been here
                }
                else {
                    visit(adjacencies[i]);
                }
            }
        }
    }

    pathList.pop();
}

visit(start);

alert(solutionCount);
</script>

8512 goals. Also someone should check if my code is right.

Check out this fiddle: http://jsfiddle.net/SoonDead/rd2GN/3/

这篇关于所有可能的行动采用5x5格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆