2D阵列(板)最短路径,得分最高

时间:2014-11-16 10:21:10

标签: c# algorithm dijkstra

我正在开发一款带2D阵列(游戏板)的新游戏。每个单元格/磁贴都有一定的分数。

enter image description here

我想要实现的是算法可以找到具有最高核心的最短路径。

所以我首先实现了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;
      }
  }

1 个答案:

答案 0 :(得分:1)

我不打算在这里给出一个完整的增强答案,但我想我可以给你一个如何实现这个目标的好建议。

您需要做的是使用单元格中的分数作为图表上的边长。进入下一个单元格将需要1或100步。

实现最长路径算法会更容易,因为您希望最大化分数。问题是,它会改为整个董事会。

enter image description here

所以你需要的是通过简单路线的负成本,所以它会尝试超过100,但不会超过其他负数单元。

enter image description here

您也可以使用最短路径算法执行此操作,但是您需要反转您的分数,以便它可以通过最短路径获得最低分数。

enter image description here

所以不要查看算法的绝对长度,而是使用值作为交叉长度。我希望这会有所帮助,最终会让你得到一个很好的算法。