Advertisement
Infiniti_Inter

22-2 # 6

May 12th, 2020
1,279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.20 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6.  
  7.  
  8. public class Graph
  9. {
  10.  
  11.     public string[] f;
  12.  
  13.     private class Node //вложенный класс для скрытия данных и алгоритмов
  14.     {
  15.         public void Show()
  16.         {
  17.             for (int i = 0; i < array.GetLength(0); ++i)
  18.             {
  19.                 for (int j = 0; j < array.GetLength(0); ++j)
  20.                     Console.Write(array[i, j] + " ");
  21.                 Console.WriteLine();
  22.             }
  23.         }
  24.         public void ExcludeNode(int k)
  25.         {
  26.             for (int i = 0; i < array.GetLength(0); ++i)
  27.                 array[i, k] = array[k, i] = 0;
  28.  
  29.         }
  30.        public static int[] p;
  31.  
  32.        public void Solve(int a, int b)
  33.        {
  34.           a--;b--;
  35.           BFS(a);
  36.           if (p[b] == -1)
  37.             {
  38.               Console.WriteLine("No path");
  39.               return;
  40.             }
  41.           int i = b;
  42.           List<int> path = new List<int>();
  43.           while (p[i] != i)
  44.           {
  45.             path.Add(i);
  46.             i = p[i];
  47.           }
  48.           path.Add(i);
  49.           path.Reverse();
  50.           Console.WriteLine($"path from {a + 1} to {b + 1}");
  51.           foreach( var v in path)
  52.             Console.Write(v + 1 + " ");
  53.           Console.WriteLine();
  54.        }
  55.        int sz;
  56.        public void BFS(int sourse)
  57.         {
  58.             for(int i = 0; i < sz; ++i)
  59.               p[i] = -1;
  60.             Queue<int> q = new Queue<int>();
  61.             q.Enqueue(sourse);
  62.             p[sourse] = sourse;
  63.             while (q.Count != 0)
  64.             {
  65.              
  66.               int v = q.Dequeue();
  67.               //Console.Write(v + " ");
  68.               for (int i = 0; i < sz; ++i)
  69.               {
  70.                 if (array[v, i] != 0 && p[i] == -1)
  71.                   {
  72.                     q.Enqueue(i);
  73.                     p[i] = v;
  74.                   }
  75.               }
  76.                
  77.             }
  78.         }
  79.         private int[,] array; //матрица смежности
  80.         public int this[int i, int j] //индексатор для обращения к матрице смежности
  81.         {
  82.             get
  83.             {
  84.                 return array[i, j];
  85.             }
  86.             set
  87.             {
  88.                 array[i, j] = value;
  89.             }
  90.         }
  91.         public bool this[int i] //индексатор для обращения к матрице меток
  92.         {
  93.             get
  94.             {
  95.                 return nov[i];
  96.             }
  97.             set
  98.             {
  99.                 nov[i] = value;
  100.             }
  101.         }
  102.         public int Size //свойство для получения размерности матрицы смежности
  103.         {
  104.             get
  105.             {
  106.                 return array.GetLength(0);
  107.             }
  108.         }
  109.         private bool[] nov; //вспомогательный массив: если i-ый элемент массива равен
  110.                             //true, то i-ая вершина еще не просмотрена; если i-ый
  111.                             //элемент равен false, то i-ая вершина просмотрена
  112.         public void NovSet() //метод помечает все вершины графа как непросмотреные
  113.         {
  114.             for (int i = 0; i < Size; i++)
  115.             {
  116.                 nov[i] = true;
  117.             }
  118.         }
  119.         //конструктор вложенного класса, инициализирует матрицу смежности и
  120.         // вспомогательный массив
  121.         public Node(int[,] a)
  122.         {
  123.             array = a;
  124.             sz = a.GetLength(0);
  125.             nov = new bool[sz];
  126.             p = new int[sz];
  127.         }
  128.  
  129.  
  130.  
  131.  
  132.     }
  133.     private Node graph; //закрытое поле, реализующее АТД «граф»
  134.  
  135.     public void Solve(int a, int b)
  136.     {
  137.       graph.Solve(a, b);
  138.     }
  139.     public Graph(string name) //конструктор внешнего класса
  140.     {
  141.         using (StreamReader file = new StreamReader($"{name}"))
  142.         {
  143.             int n = int.Parse(file.ReadLine());
  144.             int[,] a = new int[n, n];
  145.  
  146.  
  147.             for (int i = 0; i < n; i++)
  148.             {
  149.                 string line = file.ReadLine();
  150.                 string[] mas = line.Split(' ');
  151.                 for (int j = 0; j < n; j++)
  152.                 {
  153.                     a[i, j] = int.Parse(mas[j]);
  154.                 }
  155.             }
  156.             graph = new Node(a);
  157.         }
  158.     }
  159.     //метод выводит матрицу смежности на консольное окно
  160.     public void Show()
  161.     {
  162.         graph.Show();
  163.     }
  164.  
  165.  
  166.  
  167.     public void ExcludeNode(int k)
  168.     {
  169.         graph.ExcludeNode(k);
  170.     }
  171.  
  172.  
  173.     public int GetLength()
  174.     {
  175.         return graph.Size;
  176.     }
  177. }
  178. class Program
  179. {
  180.     static void Main(string[] args)
  181.     {
  182.  
  183.         Graph gr = new Graph("input.txt");
  184.         gr.Solve(1, 5);
  185.        /*
  186.        5
  187. 0 1 0 1 0
  188. 0 0 1 1 0
  189. 0 0 0 0 1
  190. 0 1 0 1 0
  191. 1 1 0 0 0
  192. */
  193.  
  194.     }
  195.  
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement