Exemple #1
0
        static void ex4()
        {
            int[,] distances = new int[10, 10];
            for (int i = 0; i < distances.GetLength(0); i++)
                for (int j = 0; j < distances.GetLength(1); j++)
                    distances[i, j] = -1;
            for (int i = 0; i < distances.GetLength(0); i++)
                distances[i, rnd.Next(0, i)] = rnd.Next(40, 120);
            for (int i = 0; i < distances.GetLength(0); i++)
                for (int j = 0; j < distances.GetLength(1); j++)
                    if (j > i)
                        distances[i, j] = distances[j, i];
            for (int i = 0; i < distances.GetLength(0); i++)
            {
                for (int j = 0; j < distances.GetLength(1); j++)
                    Console.Write("{0, -3} ", distances[i, j]);
                Console.WriteLine("\n");
            }
            Console.WriteLine();
            int city = 0, limit = 200;
            while (city < 1 || city > distances.GetLength(0))
            {
                Console.Write("Enter the number of city (from 1 to " + distances.GetLength(0) + "): ");
                city = int.Parse(Console.ReadLine());
            }
            Console.WriteLine();
            Graph1 G = new Graph1(distances, distances.GetLength(0));
            Dictionary<string, int> dist = new Dictionary<string, int>();
            Console.WriteLine("\nPaths:\n");
            for (int j = 0; j < distances.GetLength(0); j++)
                if (!dist.ContainsKey("From " + city + " to " + j + 1) && city != j + 1)
                {
                    var stackBFS = G.BFS1(city - 1, j);
                    ShowPath1(stackBFS);
                    Console.WriteLine();
                    DictionaryAdd(stackBFS, distances, ref dist, city);
                }
            Console.WriteLine("\nDistances:\n");
            foreach (var item in dist)
                if (item.Value <= limit)
                    Console.WriteLine(item.Key + " : " + item.Value + "\n");

        }