public static void Main(string[] args) { /* Please past in the following method your path file: */ MyMap.PrintBarsPerPath("input.txt"); }
/* * Creating MyMaps by reading from file. Parameter 'path' is the file to read from. */ public static ArrayList CreateMaps(string path) { ArrayList finalMaps = new ArrayList(); // To store all the maps contained in the file. try { using (StreamReader sr = File.OpenText(path)) { char[] arr; // To store location in each line. string line = string.Empty; while ((line = sr.ReadLine()) != null) { /* 1) For the field 'locations' of MyMap. */ int edges = Convert.ToInt32(line); // Number of connections per map. if (edges == -1) // End of file. { break; } Dictionary <Location, ArrayList> l = new Dictionary <Location, ArrayList>(); int finalBars = 0; String[] info; Location[] route = new Location[2]; MyMap m = new MyMap(l, finalBars, route); for (int i = 0; i < edges; i++) // Assign neighbours nodes to each location in the map. { line = sr.ReadLine(); arr = line.ToCharArray(); Location one = CreateLocFromChar(arr[0]); Location two = CreateLocFromChar(arr[2]); if (!l.ContainsKey(one) && !l.ContainsKey(two)) { ArrayList alOne = new ArrayList(); ArrayList alTwo = new ArrayList(); alOne.Add(two); l[one] = alOne; alTwo.Add(one); l[two] = alTwo; } else { l[one].Add(two); if (!l.ContainsKey(two)) { ArrayList al1 = new ArrayList(); al1.Add(one); l[two] = al1; } else { l[two].Add(one); } } } /* 2) For the fields 'finalBars' and 'route' of MyMap. */ line = sr.ReadLine(); info = line.Split(' '); finalBars = Convert.ToInt32(info[0]); m.SetFinalBars(finalBars); Location start = CreateLocFromChar(info[1][0]); Location end = CreateLocFromChar(info[2][0]); route[0] = start; route[1] = end; m.SetRoute(route); finalMaps.Add(m); // Add MyMap to the list, before creating the next ones (if any). } sr.Close(); } } catch (Exception) { Console.WriteLine("Error occurred."); } /* Console output all MyMap. */ int counter = 1; foreach (MyMap m in finalMaps) { Console.Write("Map case " + counter++ + ": "); m.Print(m); Console.WriteLine(); } return(finalMaps); }