예제 #1
0
 public CarDistance(Car car, int distance,Road road, int delay = 0 )
 {
     this.Car = car;
     this.Distance = distance;
     this.Road = road;
     this.Delay = delay;
 }
예제 #2
0
 private Direction DetermineDirection(Crossroad crossroad, Road road)
 {
     if (crossroad.North != null && crossroad.North.Id == road.Id)
         return Direction.North;
     if (crossroad.South != null && crossroad.South.Id == road.Id)
         return Direction.South;
     if (crossroad.East != null && crossroad.East.Id == road.Id)
         return Direction.East;
     if (crossroad.West != null && crossroad.West.Id == road.Id)
         return Direction.West;
     throw new Exception("Cannot determine direction");
 }
예제 #3
0
        public static void FillRoadsAndCrossroads(ProblemInstance problemInstance, String inputFileName)
        {
            try
            {
                var reader = File.OpenText(inputFileName);

                var ySize = Convert.ToInt32(reader.ReadLine());
                var xSize = Convert.ToInt32(reader.ReadLine());
                var tileLength = Convert.ToInt32(reader.ReadLine());

                char[,] tiles = new char[ySize, xSize];
                var crossroadsDict = new Dictionary<Coordinates, Crossroad>();
                var roadsList = new List<Road>();

                for (var i = 0; i < ySize; ++i)
                {
                    var line = reader.ReadLine();
                    for (var j = 0; j < xSize; ++j)
                    {
                        var tile = line[j];
                        tiles[i, j] = tile;
                        if (tile == 'X')
                        {
                            crossroadsDict.Add(new Coordinates(i, j), new Crossroad(i * tileLength, j * tileLength));
                        }
                    }
                }

                var coordinates = crossroadsDict.Keys;

                foreach (Coordinates c in coordinates)
                {
                    Crossroad crossroad = crossroadsDict[c];

                    if (c.Y < ySize - 1)
                    {
                        int lanes = 1;
                        int iterY = c.Y + 1;
                        while (true)
                        {
                            if (iterY >= ySize)
                            {
                                var road = new Road((iterY - c.Y - 1) * tileLength, crossroad, null, lanes, Orientation.NorthSouth);
                                crossroad.South = road;
                                roadsList.Add(road);
                                break;
                            }
                            char tile = tiles[iterY, c.X];
                            if (tile == '-')
                            {
                                ++iterY;
                            }
                            else if (tile == '=')
                            {
                                ++iterY;
                                lanes = 2;
                            }
                            else if (tile == 'X')
                            {
                                var endCrossroad = crossroadsDict[new Coordinates(iterY, c.X)];
                                var road = new Road((iterY - c.Y) * tileLength, crossroad, endCrossroad, lanes, Orientation.NorthSouth);
                                crossroad.South = road;
                                endCrossroad.North = road;
                                roadsList.Add(road);
                                break;
                            }
                            else
                            {
                                break;
                            }
                        }
                    }

                    if (c.X < xSize - 1)
                    {
                        int lanes = 1;
                        int iterX = c.X + 1;
                        while (true)
                        {
                            if (iterX >= xSize)
                            {
                                var road = new Road((iterX - c.X - 1) * tileLength, crossroad, null, lanes, Orientation.EastWest);
                                crossroad.East = road;
                                roadsList.Add(road);
                                break;
                            }
                            char tile = tiles[c.Y, iterX];
                            if (tile == '-')
                            {
                                ++iterX;
                            }
                            else if (tile == '=')
                            {
                                ++iterX;
                                lanes = 2;
                            }
                            else if (tile == 'X')
                            {
                                var endCrossroad = crossroadsDict[new Coordinates(c.Y, iterX)];
                                var road = new Road((iterX - c.X) * tileLength, crossroad, endCrossroad, lanes, Orientation.EastWest);
                                crossroad.East = road;
                                endCrossroad.West = road;
                                roadsList.Add(road);
                                break;
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                }

                for (int i = 0; i < ySize; ++i)
                {
                    int lanes = 1;
                    int iterX = 0;
                    while (true) {
                        char tile = tiles[i, iterX];
                        if (tile == '-')
                        {
                            ++iterX;
                        }
                        else if (tile == '=')
                        {
                            ++iterX;
                            lanes = 2;
                        }
                        else if (tile == 'X')
                        {
                            if (iterX == 0)
                            {
                                break;
                            }
                            var endCrossroad = crossroadsDict[new Coordinates(i, iterX)];
                            var road = new Road(iterX * tileLength, null, endCrossroad, lanes, Orientation.EastWest);
                            endCrossroad.West = road;
                            roadsList.Add(road);
                            break;
                        }
                        else
                        {
                            break;
                        }
                    }
                }

                for (int i = 0; i < xSize; ++i)
                {
                    int lanes = 1;
                    int iterY = 0;
                    while (true)
                    {
                        char tile = tiles[iterY, i];
                        if (tile == '-')
                        {
                            ++iterY;
                        }
                        else if (tile == '=')
                        {
                            ++iterY;
                            lanes = 2;
                        }
                        else if (tile == 'X')
                        {
                            if (iterY == 0)
                            {
                                break;
                            }
                            var endCrossroad = crossroadsDict[new Coordinates(iterY, i)];
                            var road = new Road(iterY * tileLength, null, endCrossroad, lanes, Orientation.NorthSouth);
                            endCrossroad.North = road;
                            roadsList.Add(road);
                            break;
                        }
                        else
                        {
                            break;
                        }
                    }
                }

                problemInstance.Crossroads = new List<Crossroad>(crossroadsDict.Values);
                problemInstance.Roads = roadsList;
                problemInstance.Routes = new List<Route>();
            }

            catch (Exception e)
            {
                Console.Out.Write(e.StackTrace);
            }
        }