Advertisement
StreetKatya

1

Jun 26th, 2022
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.47 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  7. using System.Threading.Tasks;
  8.  
  9. namespace _3zadacha
  10. {
  11. class Program
  12. {
  13. static void Main(string[] args)
  14. {
  15. Graph g;
  16. List<string> names;
  17. using (var sr = new StreamReader("input.txt"))
  18. {
  19. List<List<int>> tunnels = new List<List<int>>();
  20. string exit = sr.ReadLine();
  21. string str1 = sr.ReadToEnd();
  22. int n = countPeregorodok(str1);
  23. names = str1.Split(new char[] { ' ', '-', '#' }, StringSplitOptions.RemoveEmptyEntries).ToList();
  24. for (int i = 0; i < n; i++)
  25. {
  26. tunnels.Add(new List<int>());
  27. }
  28.  
  29.  
  30. int countBunkers, countExits = 1, countTunnels = n;
  31. List<int> exits = new List<int>();
  32.  
  33. for (int i = 0; i < countTunnels; i++)
  34. {
  35. tunnels.Add(new List<int>());
  36. }
  37. sr.
  38. string[] str;
  39. for (int i = 0; i < countTunnels; i++)
  40. {
  41. str = sr.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  42. tunnels[int.Parse(str[0]) - 1].Add(int.Parse(str[1]) - 1);
  43. tunnels[int.Parse(str[1]) - 1].Add(int.Parse(str[0]) - 1);
  44. }
  45.  
  46. g = new Graph(names.Count, countTunnels, exits, tunnels);
  47. }
  48.  
  49. int[] minDistance = SearchPath(g);
  50. using (var sw = new StreamWriter("output.txt"))
  51. {
  52. foreach (int dist in minDistance)
  53. {
  54. sw.Write(names[dist] + " ");
  55. }
  56. }
  57. }
  58. public static int countPeregorodok(string str)
  59. {
  60. int count = 0;
  61. Regex reg = new Regex(@"[-]");
  62. count = reg.Matches(str).Count;
  63. return count;
  64. }
  65. public static int[] SearchPath(Graph g)
  66. {
  67. int[] minDistToPoint = new int[g.countPoints];
  68. for (int i = 0; i < g.countPoints; i++)
  69. {
  70. minDistToPoint[i] = -1;
  71. }
  72.  
  73. Queue<int> pathToPoint = new Queue<int>();
  74. for (int j = 0; j < g.specificPoints.Count; j++)
  75. {
  76. pathToPoint.Enqueue(g.specificPoints[j]);
  77. minDistToPoint[g.specificPoints[j]] = 0;
  78. }
  79.  
  80. while (pathToPoint.Count != 0)
  81. {
  82. int currentPoint = pathToPoint.Peek();
  83. pathToPoint.Dequeue();
  84.  
  85. for (int k = 0; k < g.listPoints[currentPoint].Count; k++)
  86. {
  87. if (minDistToPoint[g.listPoints[currentPoint][k]] == -1)
  88. {
  89. pathToPoint.Enqueue(g.listPoints[currentPoint][k]);
  90. minDistToPoint[g.listPoints[currentPoint][k]] = minDistToPoint[currentPoint] + 1;
  91. }
  92. }
  93. }
  94. return minDistToPoint;
  95. }
  96. }
  97. public class Graph
  98. {
  99. private int __countPoints;
  100. public int countPoints
  101. {
  102. get { return __countPoints; }
  103. set
  104. {
  105. if (value < 1)
  106. {
  107. throw new ArgumentException("Неверный ввод данных");
  108. }
  109. else __countPoints = value;
  110. }
  111. }
  112. private int __countEdges;
  113. public int countEdges
  114. {
  115. get { return __countEdges; }
  116. set
  117. {
  118. if (value < 0)
  119. {
  120. throw new ArgumentException("Неверный ввод данных");
  121. }
  122. else __countEdges = value;
  123. }
  124. }
  125. public List<List<int>> listPoints = new List<List<int>>();
  126. public List<int> specificPoints = new List<int>();
  127. public Graph(int countPoints, int countEdges, List<int> specificPoints, List<List<int>> listPoints)
  128. {
  129. this.countPoints = countPoints;
  130. this.countEdges = countEdges;
  131. this.specificPoints = specificPoints;
  132. this.listPoints = listPoints;
  133. }
  134. }
  135. }
  136.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement