C# 得分最高的2D阵列(板)最短路径
我正在开发一款带有2D阵列(游戏板)的新游戏。每个单元格/磁贴都有一定数量的点 我想要实现的是,一个算法可以找到具有最高核心的最短路径 所以我首先实现了Dijkstra算法(下面的源代码)来寻找从起点到终点的最短路径(红色路线)。这很有效 我的问题是:如何扩展我的算法,使其确定得分最高的最短路径(绿色路径) 提前谢谢你C# 得分最高的2D阵列(板)最短路径,c#,algorithm,dijkstra,C#,Algorithm,Dijkstra,我正在开发一款带有2D阵列(游戏板)的新游戏。每个单元格/磁贴都有一定数量的点 我想要实现的是,一个算法可以找到具有最高核心的最短路径 所以我首先实现了Dijkstra算法(下面的源代码)来寻找从起点到终点的最短路径(红色路线)。这很有效 我的问题是:如何扩展我的算法,使其确定得分最高的最短路径(绿色路径) 提前谢谢你 class Graph { // Dictionary<Start Point, vertices> Dictionary<char, Dic
class Graph
{
// Dictionary<Start Point, vertices>
Dictionary<char, Dictionary<char, int>> vertices = new Dictionary<char, Dictionary<char, int>>();
public void add_vertex(char name, Dictionary<char, int> edges)
{
vertices[name] = edges;
}
public List<char> shortest_path(char start, char finish)
{
var previous = new Dictionary<char, char>();
var distances = new Dictionary<char, int>();
var nodes = new List<char>();
List<char> path = null;
foreach (var vertex in vertices)
{
if (vertex.Key == start)
{
distances[vertex.Key] = 0;
}
else
{
distances[vertex.Key] = int.MaxValue;
}
nodes.Add(vertex.Key);
}
while (nodes.Count != 0)
{
nodes.Sort((x, y) => distances[x] - distances[y]);
var smallest = nodes[0];
nodes.Remove(smallest);
if (smallest == finish)
{
path = new List<char>();
while (previous.ContainsKey(smallest))
{
path.Add(smallest);
smallest = previous[smallest];
}
break;
}
if (distances[smallest] == int.MaxValue)
{
break;
}
foreach (var neighbor in vertices[smallest])
{
var alt = distances[smallest] + neighbor.Value;
if (alt < distances[neighbor.Key])
{
distances[neighbor.Key] = alt;
previous[neighbor.Key] = smallest;
}
}
}
return path;
}
}
类图
{
//字典
字典顶点=新字典();
public void add_顶点(字符名、字典边)
{
顶点[名称]=边;
}
公共列表最短路径(字符开始、字符结束)
{
var previous=新字典();
var距离=新字典();
var节点=新列表();
列表路径=空;
foreach(顶点中的var顶点)
{
如果(vertex.Key==开始)
{
距离[顶点.关键点]=0;
}
其他的
{
距离[顶点.关键点]=int.MaxValue;
}
nodes.Add(vertex.Key);
}
while(nodes.Count!=0)
{
nodes.Sort((x,y)=>距离[x]-距离[y]);
var最小=节点[0];
移除(最小的);
如果(最小==完成)
{
路径=新列表();
while(先前的ContainsKey(最小))
{
path.Add(最小);
最小的=先前的[最小的];
}
打破
}
if(距离[最小]==int.MaxValue)
{
打破
}
foreach(顶点中的var邻居[最小])
{
var alt=距离[最小值]+相邻值;
if(alt<距离[邻居键])
{
距离[neighbor.Key]=alt;
前一个[邻居.键]=最小的;
}
}
}
返回路径;
}
}
更新:
我已经尝试过类似的方法,但似乎不起作用:
foreach (var neighbour in _vertices[smallest])
{
// The variable alt is the length of the path from the root node to the neighbor node if it were to go through _vertices[smallest].
var alt = pointInfos[smallest].Distance + neighbour.Value.Distance;
var altScore = pointInfos[smallest].Points + neighbour.Value.Points;
if (alt <= pointInfos[neighbour.Key].Distance && altScore > pointInfos[neighbour.Key].Points) // A shorter path with higher points to neighbour has been found
{
pointInfos[neighbour.Key] = new PointInfo(alt, altScore);
previous[neighbour.Key] = smallest;
}
}
foreach(在_顶点中的var邻居[最小])
{
//变量alt是从根节点到相邻节点的路径长度(如果要通过_个顶点[最小])。
var alt=pointInfos[最小]。距离+邻居.Value.Distance;
var altScore=pointInfos[minimable].Points+nexter.Value.Points;
if(alt pointInfos[Neighbor.Key].Points)//找到了一条到邻居点较高的较短路径
{
PointInfo[Neighbor.Key]=新的PointInfo(alt、altScore);
前一个[邻居.键]=最小的;
}
}
我不打算在这里给出一个完整的增强型答案,但我想我可以给你一个关于如何实现这一点的好提示
您需要做的是使用单元格中的分数作为图形上的边长度。进入下一个单元需要1到100步
实现最长路径算法会更容易,因为您希望最大化分数。问题是,它随后将覆盖整个电路板
所以你需要的是一个负的成本,通过简单的路线,所以它将尝试超过100,但不超过其余的负细胞
你也可以用最短路径算法来实现这一点,但是你需要反转你的分数,这样它就可以用最低的分数通过最短路径
因此,不要查看算法的绝对长度,而是使用这些值作为交叉长度。我希望这会有所帮助,并最终为您提供一个好的算法。您的绿色路径不是最短的。没错:它是得分最高的最短路径!这就是我想要实现的……当你有两个标准时,你必须正确地排列它们。你是说你想先得到最高分,然后在两条路径得分相同的情况下,选择最短的路径?