簡體   English   中英

2D陣列(板)具有最高分數的最短路徑

[英]2D array (board) shortest path with highest score

我正在開發帶有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;
      }
  }

我不會在這里給出完整的增強答案,但是我想我可以為您提供實現此目標的很好的技巧。

您需要做的是將單元格中的分數用作圖形上的邊長。 轉到下一個單元格將花費您1或100步。

實施最長路徑算法會更容易,因為您想最大化得分。 問題在於它將代替整個電路板。

在此處輸入圖片說明

因此,您需要的是通過簡單路線獲得的負費用,因此它將嘗試超過100,但不會超過其余的負電池。

在此處輸入圖片說明

您也可以使用最短路徑算法來執行此操作,但是隨后您需要反轉分數,以便它可以越過分數最低的最短路徑。

在此處輸入圖片說明

因此,請勿查看算法的絕對長度,而應將這些值用作交叉的長度。 我希望這會有所幫助,並最終使您獲得一個不錯的算法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM