Beispiel #1
0
        public string GetPath()
        {
            int   firstId    = _rows[_from].GetId();
            int   secondId   = _rows[_from].GetElement(_to).GetId();
            ICity firstCity  = null;
            ICity secondCity = null;

            for (int i = 0; i < _graph.Count; i++)
            {
                ICity currentCity1 = _graph[i];
                if (currentCity1.Id == firstId)
                {
                    firstCity = currentCity1;
                }
                if (currentCity1.Id == secondId)
                {
                    secondCity = currentCity1;
                }
            }
            if (firstCity == null | secondCity == null)
            {
                return("");
            }
            int sum = firstCity.GetDistTo(secondCity.Id);

            if (sum >= 5000)
            {
                return("Отсутствует какой-либо путь между данными вершинами");
            }
            string        path        = "";
            ICityStorage  qeue        = secondCity.GetNeighbors();//new
            ICity         currentCity = secondCity;
            List <string> pathList    = new List <string>();

            while (sum > 0)
            {
                for (int i = 0; i < qeue.Count; i++)
                {
                    ICity tmpCity = qeue[i];
                    int   weight  = tmpCity.GetWeightToNeighbor(currentCity.Id);
                    if (firstCity.GetDistTo(tmpCity.Id) == firstCity.GetDistTo(currentCity.Id) - weight)
                    {
                        //path += currentCity.GetId() + " <- ";
                        pathList.Add(currentCity.Id.ToString());
                        sum        -= weight;
                        currentCity = tmpCity;
                        break;
                    }
                }
                qeue = currentCity.GetNeighbors(); //new
            }
            for (int i = pathList.Count - 1; i >= 0; i--)
            {
                path += " -> " + pathList[i];
            }
            return(firstCity.Id.ToString() + path);
        }