Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.69 KB | None | 0 0
  1. class AllShorestPaths
  2. {
  3. static string[,] matrix={
  4. {"0","0","0","X ","0","0","X ","X "},
  5. {"X ","0","0","X ","0","0","0","X "},
  6. {"0","0","0","0","X ","0","X ","X "},
  7. {"X ","0","0","X ","0","0","0","0"},
  8. {"0","0","X ","0","0","0","X ","X "},
  9. {"X ","0","0","0","X ","0","0","X "}
  10. };
  11. class Node
  12. {
  13. private int row;
  14. private int col;
  15. private int depth;
  16.  
  17. public int Row
  18. {
  19. get
  20. {
  21. return this.row;
  22. }
  23.  
  24. set
  25. {
  26. if (value >= 0)
  27. this.row = value;
  28.  
  29. else
  30. {
  31. throw new ArgumentException("Row can not be negative!");
  32. }
  33. }
  34. }
  35.  
  36. public int Col
  37. {
  38. get
  39. {
  40. return this.col;
  41. }
  42.  
  43. set
  44. {
  45. if (value >= 0)
  46. this.col = value;
  47.  
  48. else
  49. {
  50. throw new ArgumentException("Row can not be negative!");
  51. }
  52. }
  53. }
  54.  
  55. public int Depth
  56. {
  57. get
  58. {
  59. return this.depth ;
  60. }
  61.  
  62. set
  63. {
  64. this .depth =value ;
  65. }
  66. }
  67.  
  68. public Node(int row, int col, int depth)
  69. {
  70. this.Row = row;
  71. this.Col = col;
  72. this.Depth = depth;
  73. }
  74. }
  75.  
  76. static bool IsInRange(int row, int col)
  77. {
  78. if (row < 0 || row >= matrix.GetLength(0) || col < 0 || col >= matrix.GetLength(1))
  79. return false ;
  80. return true;
  81. }
  82.  
  83. static void FindShortestPathsBFS(int startRow, int startCol)
  84. {
  85. matrix[startRow, startCol] = "* ";
  86. bool[,] visited = new bool[matrix.GetLength(0), matrix.GetLength(1)];
  87. Queue<Node> q = new Queue<Node>();
  88. q.Enqueue (new Node (startRow ,startCol ,0));
  89. visited [startRow ,startCol ]=true;
  90.  
  91. while (q.Count > 0)
  92. {
  93. Node current = q.Dequeue();
  94. int row = current.Row;
  95. int col = current.Col;
  96. visited[row, col] = true;
  97.  
  98. if (!(row == startRow && col == startCol))
  99. {
  100. matrix[row, col] = current.Depth+ " ";
  101. }
  102.  
  103. if (IsInRange(row + 1, col) && matrix[row + 1, col] == "0" && !visited[row+1, col])
  104. {
  105. q.Enqueue (new Node (row +1,col,current .Depth +1));
  106. }
  107.  
  108. if (IsInRange(row - 1, col) && matrix[row - 1, col] == "0" && !visited[row - 1, col])
  109. {
  110. q.Enqueue(new Node(row - 1, col,current .Depth +1));
  111. }
  112.  
  113. if (IsInRange(row , col+1) && matrix[row , col+1] == "0" && !visited[row , col+1])
  114. {
  115. q.Enqueue(new Node(row , col+1, current.Depth + 1));
  116. }
  117.  
  118. if (IsInRange(row , col-1) && matrix[row , col-1] == "0" && !visited[row, col-1])
  119. {
  120. q.Enqueue(new Node(row , col-1, current.Depth + 1));
  121. }
  122. }
  123. FillUnreachable();
  124. PrintMatrix();
  125. }
  126.  
  127. static void FillUnreachable()
  128. {
  129. for (int row = 0; row < matrix.GetLength(0); row++)
  130. {
  131. for (int col = 0; col < matrix.GetLength(1); col++)
  132. {
  133. if (matrix[row, col] == "0")
  134. matrix[row, col] = "u";
  135. }
  136. }
  137. }
  138.  
  139. static void PrintMatrix()
  140. {
  141. for (int row = 0; row < matrix.GetLength(0); row++)
  142. {
  143. for (int col = 0; col < matrix.GetLength(1); col++)
  144. {
  145. Console.Write("{0,5}",matrix[row, col]);
  146. }
  147. Console.WriteLine();
  148. }
  149.  
  150. }
  151.  
  152. static void Main(string[] args)
  153. {
  154. FindShortestPathsBFS(0,0);
  155. }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement