Algorithm 图集长度上的随机路径,无交叉,无死角

Algorithm 图集长度上的随机路径,无交叉,无死角,algorithm,graph,depth-first-search,path-finding,Algorithm,Graph,Depth First Search,Path Finding,我正在做一个8宽5高的游戏。我有一个'蛇'的功能,需要进入网格和“走”周围的一个设定的距离(例如20)。蛇的移动有一定的限制: 它需要经过预定数量的块(20) 它不能越过自身或双回(没有死胡同) 目前,我使用的是随机深度优先搜索,但我发现它偶尔会返回自身(穿过自己的路径),不确定这是否是最好的方法 考虑的选项:我已经考虑过使用A*,但是我正在努力找到一种好的方法来实现它,而没有预先确定的目标和上述条件。我还考虑过添加一种启发式方法,以支持不在网格外部的块,但我不确定这两种方法能否解决当前的问

我正在做一个8宽5高的游戏。我有一个'蛇'的功能,需要进入网格和“走”周围的一个设定的距离(例如20)。蛇的移动有一定的限制:

  • 它需要经过预定数量的块(20)
  • 它不能越过自身或双回(没有死胡同)

目前,我使用的是随机深度优先搜索,但我发现它偶尔会返回自身(穿过自己的路径),不确定这是否是最好的方法

考虑的选项:我已经考虑过使用A*,但是我正在努力找到一种好的方法来实现它,而没有预先确定的目标和上述条件。我还考虑过添加一种启发式方法,以支持不在网格外部的块,但我不确定这两种方法能否解决当前的问题

非常感谢您的帮助,如有必要,我可以添加更多详细信息或代码:

public List<GridNode> RandomizedDepthFirst(int distance, GridNode startNode)
{
    Stack<GridNode> frontier = new Stack<GridNode>();
    frontier.Push(startNode);
    List<GridNode> visited = new List<GridNode>();
    visited.Add(startNode);
    while (frontier.Count > 0 && visited.Count < distance)
    {
        GridNode current = frontier.Pop();

        if (current.nodeState != GridNode.NodeState.VISITED)
        {
            current.nodeState = GridNode.NodeState.VISITED;

            GridNode[] vals = current.FindNeighbours().ToArray();
            List<GridNode> neighbours = new List<GridNode>();
            foreach (GridNode g in vals.OrderBy(x => XMLReader.NextInt(0,0)))
            {
                neighbours.Add(g);
            }
            foreach (GridNode g in neighbours)
            {
                frontier.Push(g);
            }

            if (!visited.Contains(current))
            {
                visited.Add(current);
            }
        }

    }
    return visited;
}
public List RandomizedDepthFirst(int-distance,GridNode-startNode)
{
堆栈边界=新堆栈();
边界推送(startNode);
访问列表=新列表();
已访问。添加(startNode);
while(frontier.Count>0&&visted.CountXMLReader.NextInt(0,0))中的GridNode g)
{
添加(g);
}
foreach(邻居中的GridNode g)
{
前推(g);
}
如果(!visted.Contains(当前))
{
已访问。添加(当前);
}
}
}
回访;
}

一种解释回溯的简单方法是使用递归dfs搜索。
考虑下面的图表:

以及dfs搜索的java实现,回溯时从路径中删除节点(注意注释。联机运行):

import java.util.ArrayList;
导入java.util.Collections;
导入java.util.List;
导入java.util.Stack;
公共类图{
//所有图节点
私有节点[]个节点;
公共图(int numberOfNodes){
节点=新节点[numberOfNodes];
//构造节点
for(int i=0;i
输出:

testing node 1 testing node 6 testing node 7 testing node 8 testing node 2 testing node 5 testing node 3 testing node 4 [1, 2, 5, 3, 4, 0] 测试节点1 测试节点6 测试节点7 测试节点8 测试节点2 测试节点5 测试节点3 测试节点4 [1, 2, 5, 3, 4, 0]
请注意,对死端节点6、7、8进行了测试,但未包括在最终路径中。

“我发现它偶尔会自行返回”我假设这是由于常见的“回溯”造成的。我还假设(这是我在没有mcve的情况下所能做的)它意味着t testing node 1 testing node 6 testing node 7 testing node 8 testing node 2 testing node 5 testing node 3 testing node 4 [1, 2, 5, 3, 4, 0]