Exemple #1
0
 /// <summary>
 /// Проверяет, содержит ли список станция указанную станцию
 /// </summary>
 /// <param name="stationsList"></param>
 /// <param name="stationToCheck"></param>
 /// <returns></returns>
 private bool IsContains(List <IStation> stationsList, IStation stationToCheck)
 {
     foreach (IStation station in stationsList)
     {
         if (station.GetStationID() == stationToCheck.GetStationID())
         {
             return(true);
         }
     }
     return(false);
 }
 public int GetLevelOfStation(IStation station)
 {
     for (int i = stations.Count - 1; i >= 0; i--)
     {
         if (stations[i].GetStationID() == station.GetStationID())
         {
             return(levels[i]);
         }
     }
     return(-1);
 }
Exemple #3
0
        /// <summary>
        /// Ищет путь от станции А к станции Б
        /// </summary>
        /// <param name="stationFrom"></param>
        /// <param name="stationTo"></param>
        /// <param name="pastStation"></param>
        /// <returns></returns>
        public List <IStation> FindPath(IStation stationFrom, IStation stationTo)
        {
            Queue <IStation> searchQueue = new Queue <IStation>();
            List <IStation>  searched    = new List <IStation>();

            ITempStationsContainer pathContainer = new StationsContainer();
            int currentLevel = 0;

            IStation stationFrom_Temp = stationFrom;

            searchQueue.Enqueue(stationFrom_Temp);
            searched.Add(stationFrom_Temp);
            pathContainer.AddToContainer(stationFrom_Temp, currentLevel);

            bool IsPathFound = false;

            while (searchQueue.Count > 0)
            {
                stationFrom_Temp = searchQueue.Dequeue();


                foreach (IStation station in stationFrom_Temp.GetConnectedStations())
                {
                    if (!IsContains(searched, station))
                    {
                        if (station.GetStationID() == stationTo.GetStationID())
                        {
                            IsPathFound = true;
                        }
                        else
                        {
                            searchQueue.Enqueue(station);
                            searched.Add(station);
                            currentLevel = SetStationLevel(pathContainer, station, currentLevel);
                            pathContainer.AddToContainer(station, currentLevel);
                        }
                    }
                }

                if (IsPathFound)
                {
                    break;
                }
            }
            return(FormPath(pathContainer, stationFrom, stationTo, currentLevel));
        }