Advertisement
AvengersAssemble

Communication

Feb 19th, 2014
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.48 KB | None | 0 0
  1. using System;
  2. using Unit4.CollectionsLib;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace ConsoleApplication1
  7. {
  8.     class Program
  9.     {
  10.         public static void PrintPath(int[] path)
  11.         {
  12.             int i = 0;
  13.             while (path[i] > 0)
  14.             {
  15.                 Console.Write(path[i] + " ");
  16.                 i++;
  17.             }
  18.         }
  19.         public static int[] BuildPath(int[,] arr, int firstSelection, int station, int call)
  20.         {
  21.             int nextStation = 0;
  22.             int countPath = 0;
  23.             int[] path = new int[arr.GetLength(0)];
  24.             for (int i = 0; i < path.Length; i++)
  25.                 path[i] = -1;
  26.             path[0] = station + 1;
  27.             path[1] = arr[station, firstSelection];
  28.             countPath = 2;
  29.             while (countPath < path.Length && path[countPath - 1] != call + 1)
  30.             {
  31.                 nextStation = arr[path[countPath - 1] - 1, 0];
  32.                 if (nextStation == path[countPath - 2])
  33.                     nextStation = arr[path[countPath - 1] - 1, 1];
  34.                 path[countPath] = nextStation;
  35.                 countPath++;
  36.             }
  37.             return path;
  38.         }
  39.         public static void IndirectCommunication(int[,] arr, int station, int call)
  40.         {
  41.             int[] pathOne, pathTwo;
  42.             pathOne = BuildPath(arr, 0, station, call);
  43.             pathTwo = BuildPath(arr, 1, station, call);
  44.             Console.WriteLine("\nPath one");
  45.             PrintPath(pathOne);
  46.             Console.WriteLine("\nPath two");
  47.             PrintPath(pathTwo);
  48.             Console.WriteLine();
  49.         }
  50.         public static bool DirectCommunication(int[,] arr, int station, int call)
  51.         {
  52.             return call - 1 == arr[station, 0] || call - 1 == arr[station, 1];
  53.         }
  54.         public static void Main(string[] args)
  55.         {
  56.             int[,] channels = new int[16, 2] {{5, 9}, {7, 10}, {6, 11}, {13, 16}, {1, 16}, {3, 11},
  57.                                               {2, 15}, {12, 14}, {1, 14}, {2, 15}, {3, 6}, {8, 13},
  58.                                               {4, 12}, {8, 9}, {7, 10}, {4, 5} };
  59.             for (int i = 0; i < channels.GetLength(0); i++)
  60.             {
  61.                 Console.Write("Station number " + (i + 1) +" - ");
  62.                 Console.Write(channels[i, 0] + " , " + channels[i, 1]);
  63.                 Console.WriteLine();
  64.             }
  65.             IndirectCommunication(channels, 4, 12);
  66.         }
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement