我正在开发一个带有2D阵列(棋盘)的新游戏。每个单元格/瓷砖都有一定数量的点数。

我想要实现的是,一个算法能够找到具有最高核心的最短路径。
因此,我首先实现了Dijkstra算法(下面的源代码),以找到从头到尾的最短路径(红色路由)。这个很好用。
我的问题是:如何扩展我的算法,从而确定最短路径与最高的评分(所以绿色路线)。
提前谢谢你!
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;
}
}更新:
我已经尝试过这样的方法了,但似乎不起作用:
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;
}
}发布于 2014-11-16 11:11:37
我不会在这里给出一个完全强化的答案,但我想我可以给你一个关于如何实现这一点的很好的建议。
你需要做的是使用你在单元格中的分数作为图形上的边缘长度。进入下一个单元将需要1或100步。
这将更容易实现最长路径算法,因为你想要最大的分数。问题是,这样的话,整个董事会就会被淘汰。

所以,你需要的是一个负成本,通过简单的路线,所以它将尝试超过100,但不是超过其余的负细胞。

你也可以用最短路径算法来做这件事,但是你需要倒转你的分数,这样它就可以以最低的分数超过最短的路径。

所以,不要看算法的绝对长度,而是使用值作为长度交叉。我希望这对你有帮助,并最终会给你一个很好的算法。
https://stackoverflow.com/questions/26955914
复制相似问题