Advertisement
Guest User

Untitled

a guest
Jan 15th, 2019
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.97 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. public class Program
  6. {
  7. public static int width;
  8. public static int length;
  9. public static Queue<Node> queue = new Queue<Node>();
  10.  
  11. public static void Main(string[] args)
  12. {
  13. string line = Console.ReadLine();
  14. width = (int)Char.GetNumericValue(line[0]);
  15. length = (int)Char.GetNumericValue(line[2]);
  16. var grid = new Node[width, length];
  17. int atLine = 0;
  18.  
  19.  
  20. while ((line = Console.ReadLine()) != null)
  21. {
  22. var currentLine = line.ToCharArray().Select(x => {
  23. var num = (int)Char.GetNumericValue(x);
  24. if (num < 0 || num > 9) return 0;
  25. else return num;
  26. }).ToArray();
  27. for (int j = 0; j < length; j++)
  28. {
  29. if (j >= currentLine.Length) grid[atLine, j] = new Node(0);
  30. else grid[atLine, j] = new Node(currentLine[j]);
  31. }
  32. if (++atLine == width) break;
  33. }
  34.  
  35. initGraph(grid);
  36. /*Console.WriteLine();
  37. PrintArray(grid);
  38. Console.WriteLine();
  39. PrintNodes(grid);*/
  40.  
  41. BFS(grid[0, 0], grid[width - 1, length - 1]);
  42. //PrintVisited(grid);
  43. }
  44.  
  45. private static void BFS(Node start, Node goal)
  46. {
  47. start.level = 0;
  48. start.visited = true;
  49. queue.Enqueue(start);
  50. while (queue.Count > 0)
  51. {
  52. Node node = queue.Dequeue();
  53. foreach (var n in node.nodes)
  54. {
  55. if (!n.visited)
  56. {
  57. n.level = node.level + 1;
  58. n.visited = true;
  59. queue.Enqueue(n);
  60. if (n == goal)
  61. {
  62. Console.WriteLine(n.level);
  63. return;
  64. }
  65. }
  66. }
  67. }
  68. Console.WriteLine(-1);
  69. }
  70.  
  71. public static void initGraph(Node[,] nodes)
  72. {
  73. for (int i = 0; i < nodes.GetLength(0); i++)
  74. {
  75. for (int j = 0; j < nodes.GetLength(1); j++)
  76. {
  77. var currentNode = nodes[i, j];
  78. int mvAmount = currentNode.mvAmount;
  79. if (mvAmount == 0) continue;
  80. int right = j + mvAmount;
  81. int left = j - mvAmount;
  82. int down = i + mvAmount;
  83. int up = i - mvAmount;
  84. if (right < length) currentNode.AddNode(nodes[i, right]);
  85. if (left >= 0) currentNode.AddNode(nodes[i, left]);
  86. if (down < width) currentNode.AddNode(nodes[down, j]);
  87. if (up >= 0) currentNode.AddNode(nodes[up, j]);
  88. }
  89. }
  90. }
  91.  
  92. public static void PrintArray(Node[,] array)
  93. {
  94. int i = 0;
  95. foreach (var item in array)
  96. {
  97. Console.Write(item.mvAmount + " ");
  98. if (++i % length == 0) Console.WriteLine();
  99. }
  100. }
  101.  
  102. public static void PrintNodes(Node[,] nodes)
  103. {
  104. int i = 0;
  105.  
  106. foreach (var n in nodes)
  107. {
  108. Console.Write(n.nodes.Count + " ");
  109. if (++i % length == 0) Console.WriteLine();
  110. }
  111. }
  112.  
  113. public static void PrintVisited(Node[,] nodes)
  114. {
  115. int i = 0;
  116.  
  117. foreach (var n in nodes)
  118. {
  119. Console.Write(n.visited + " ");
  120. if (++i % length == 0) Console.WriteLine();
  121. }
  122. }
  123.  
  124. public class Node
  125. {
  126. public int level { get; set; }
  127. public bool visited { get; set; }
  128. public int mvAmount { get; set; }
  129. public List<Node> nodes = new List<Node>();
  130. public Node(int mvAmount)
  131. {
  132. this.mvAmount = mvAmount;
  133. visited = false;
  134. }
  135.  
  136. public void AddNode(Node n)
  137. {
  138. nodes.Add(n);
  139. }
  140. }
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement