本文介绍了我怎样才能找到一个迷宫最短路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的迷宫是一个int数组有两个维度, INT迷宫[] [] 包含 0,1,START(2),目标( 3)。我想打印的最短路径。

我有一个功能,但它不显示的最短路径,但一个路径到端:

 布尔RenderThread :: find_path(INT X,int y)对
{
    INT maze_size = mmaze->尺寸* 2;

    如果(X< 0 || X  -  GT; maze_size ||Ÿ℃,|| Y'GT; maze_size)返回FALSE;

    如果(toSolve1-> maze_data [Y] [X] == G)返回TRUE;

    如果(toSolve1-> maze_data [Y] [X] =路径和放大器;!&安培; toSolve1-> maze_data [Y] [X] = S!)返回FALSE;

    toSolve1-> setRed(Y,X);


    如果(find_path(X,Y  -  1)== TRUE)返回TRUE;

    如果(find_path(X + 1,Y)== TRUE)返回TRUE;

    如果(find_path(X,Y + 1)== TRUE)返回TRUE;

    如果(find_path(X  -  1,Y)== TRUE)返回TRUE;

    toSolve1->的setpath(Y,X);

    返回FALSE;
}
 

解决方案

我会建议 A *搜索算法。

伪code

 函数A *(启动,目标)
    closedset:=空集//节点集已经评估。
    openset:= {开始} //该组的暂时性节点进行评估,最初包含起始节点
    came_from:=空地图//导航节点的映射。

    g_score [开始] = 0 //成本从开始沿最有名的路径。
    //估计总成本从开始到到y的目标。
    f_score [开始]:= g_score [开始] + heuristic_cost_estimate(启动,目标)

    而openset不是空
        当前:=在openset具有最低f_score []值的节点
        如果电流=目标
            返回reconstruct_path(came_from,目标)

        删除openset电流
        增加电流closedset
        在neighbor_nodes每个邻居(电流)
            如果邻居closedset
                继续
            tentative_g_score:= g_score [现行] + dist_between(电流,邻居)

            如果邻居不openset或tentative_g_score< = g_score [邻居]
                came_from [邻居]:=电流
                g_score [邻居]:= tentative_g_score
                f_score [邻居]:= g_score [邻居] + heuristic_cost_estimate(邻居,目标)
                如果邻居不openset
                    添加邻居openset

    回报失败

功能reconstruct_path(came_from,CURRENT_NODE)
    如果came_from [CURRENT_NODE]中集
        电话号码:= reconstruct_path(came_from,came_from [CURRENT_NODE])
        回报(P + CURRENT_NODE)
    其他
        回报CURRENT_NODE
 

My maze is a int array with two dimensions, int maze[][] containing 0,1,START(2),GOAL(3). I want to print the shortest path.

I have a function but it doesn't display the shortest path but one path to the end:

bool RenderThread::find_path(int x, int y)
{
    int maze_size=mmaze->size*2;

    if ( x < 0 || x > maze_size  || y < 0 || y > maze_size  ) return FALSE;

    if ( toSolve1->maze_data[y][x] == G ) return TRUE;

    if ( toSolve1->maze_data[y][x] != PATH && toSolve1->maze_data[y][x] != S ) return FALSE;

    toSolve1->setRed(y,x);


    if ( find_path(x, y - 1) == TRUE ) return TRUE;

    if ( find_path(x + 1, y) == TRUE ) return TRUE;

    if ( find_path(x, y + 1) == TRUE ) return TRUE;

    if ( find_path(x - 1, y) == TRUE ) return TRUE;

    toSolve1->setPath(y,x);

    return FALSE;
}
解决方案

I would recommend the A* search algorithm.

Pseudocode:

function A*(start,goal)
    closedset := the empty set    // The set of nodes already evaluated.
    openset := {start}    // The set of tentative nodes to be evaluated, initially containing the start node
    came_from := the empty map    // The map of navigated nodes.

    g_score[start] := 0    // Cost from start along best known path.
    // Estimated total cost from start to goal through y.
    f_score[start] := g_score[start] + heuristic_cost_estimate(start, goal)

    while openset is not empty
        current := the node in openset having the lowest f_score[] value
        if current = goal
            return reconstruct_path(came_from, goal)

        remove current from openset
        add current to closedset
        for each neighbor in neighbor_nodes(current)
            if neighbor in closedset
                continue
            tentative_g_score := g_score[current] + dist_between(current,neighbor)

            if neighbor not in openset or tentative_g_score <= g_score[neighbor]
                came_from[neighbor] := current
                g_score[neighbor] := tentative_g_score
                f_score[neighbor] := g_score[neighbor] + heuristic_cost_estimate(neighbor, goal)
                if neighbor not in openset
                    add neighbor to openset

    return failure

function reconstruct_path(came_from, current_node)
    if came_from[current_node] in set
        p := reconstruct_path(came_from, came_from[current_node])
        return (p + current_node)
    else
        return current_node

这篇关于我怎样才能找到一个迷宫最短路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 06:33