Esempio n. 1
0
        internal void Process(List <CityConnectedContract> cityList)
        {
            StepCount += 1;

            var newList = new List <CityConnectedContract>();

            foreach (var item in cityList)
            {
                if (item.IsCityConnected)
                {
                    continue;
                }

                var cityConnect = SelectAvailableCityToLink(cityList, item);

                if (cityConnect != null)
                {
                    var tempSubCity = new CityConnectedContract {
                        CityAvaliableRoadList = new List <CityConnectedContract>()
                    };
                    tempSubCity.CityAvaliableRoadList.AddRange(cityConnect.CityAvaliableRoadList.Where(x => x.CityId != item.CityId).ToList());
                    tempSubCity.CityAvaliableRoadList.AddRange(item.CityAvaliableRoadList.Where(x => x.CityId != cityConnect.CityId).ToList());

                    newList.Add(new CityConnectedContract
                    {
                        CityId                = item.CityId,
                        CityConnectTo         = 0,
                        IsCityConnected       = false,
                        CityAvaliableRoadList = tempSubCity.CityAvaliableRoadList
                    });

                    item.IsCityConnected        = true;
                    item.CityConnectTo          = cityConnect.CityId;
                    cityConnect.IsCityConnected = true;
                    cityConnect.CityConnectTo   = item.CityId;
                }
                else
                {
                    newList.Add(new CityConnectedContract
                    {
                        CityId                = item.CityId,
                        CityConnectTo         = 0,
                        IsCityConnected       = false,
                        CityAvaliableRoadList = item.CityAvaliableRoadList
                    });
                }
            }

            var isAllCityLinked = cityList.All(x => x.IsCityConnected) && newList.Count == 1;

            if (isAllCityLinked)
            {
                Console.WriteLine("City Count: " + CityCount);
                Console.Write("Minimum step count to link all city : " + StepCount);
            }
            else
            {
                Process(newList);
            }
        }
Esempio n. 2
0
        internal CityConnectedContract SelectAvailableCityToLink(List <CityConnectedContract> cityList, CityConnectedContract city)
        {
            var minCount  = 999;
            var indisCity = 0;
            CityConnectedContract minCity = null;

            foreach (var item in city.CityAvaliableRoadList)
            {
                if (item.IsCityConnected)
                {
                    continue;
                }

                for (var i = 0; i < cityList.Count; i++)
                {
                    if (item.CityId == cityList[i].CityId)
                    {
                        indisCity = i;
                        break;
                    }
                }

                var minItemCount = cityList[indisCity].CityAvaliableRoadList.Count;
                if (minItemCount < minCount)
                {
                    minCount = minItemCount;
                    minCity  = cityList[indisCity];
                }
            }
            return(minCity);
        }