Advertisement
Guest User

DistanceInLabyrinth

a guest
Feb 21st, 2016
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.50 KB | None | 0 0
  1. namespace _8.DistanceInLabyrinth
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.  
  6.     public class DistanceInLabyrinthMain
  7.     {
  8.         private static string[,] matrix =
  9.         {
  10.             {"0","0","0","X","0","X"},
  11.             {"0","X","0","X","0","X"},
  12.             {"0","*","X","0","X","0"},
  13.             {"0","X","0","0","0","0"},
  14.             {"0","0","0","X","X","0"},
  15.             {"0","0","0","X","0","X"}
  16.         };
  17.  
  18.         public static void Main()
  19.         {
  20.             var startingCell = FindStartingCell();
  21.  
  22.             bool[,] visited = new bool[matrix.GetLength(0), matrix.GetLength(1)];
  23.             visited[startingCell.X, startingCell.Y] = true;
  24.             var queue = new Queue<Cell>();
  25.             queue.Enqueue(startingCell);
  26.  
  27.             FillCells(queue, visited);
  28.  
  29.             PrintMatrix();
  30.         }
  31.  
  32.         private static void PrintMatrix()
  33.         {
  34.             for (int i = 0; i < matrix.GetLength(0); i++)
  35.             {
  36.                 for (int j = 0; j < matrix.GetLength(1); j++)
  37.                 {
  38.                     Console.Write(matrix[i, j] + " ");
  39.                 }
  40.  
  41.                 Console.WriteLine();
  42.             }
  43.         }
  44.  
  45.         private static void FillCells(Queue<Cell> queue, bool[,] visited)
  46.         {
  47.             while (queue.Count > 0)
  48.             {
  49.                 var cell = queue.Dequeue();
  50.                 var x = cell.X;
  51.                 var y = cell.Y;
  52.                 var level = cell.Level;
  53.                 matrix[x, y] = level.ToString();
  54.  
  55.                 if (x - 1 >= 0 && matrix[x - 1, y] != "X" && !visited[x - 1, y])
  56.                 {
  57.                     queue.Enqueue(new Cell() {X = x - 1, Level = level + 1, Y = y});
  58.                     visited[x - 1, y] = true;
  59.                 }
  60.  
  61.                 if (x + 1 < matrix.GetLength(0) && matrix[x + 1, y] != "X" && !visited[x + 1, y])
  62.                 {
  63.                     queue.Enqueue(new Cell() {X = x + 1, Level = level + 1, Y = y});
  64.                     visited[x + 1, y] = true;
  65.                 }
  66.  
  67.                 if (y - 1 >= 0 && matrix[x, y - 1] != "X" && !visited[x, y - 1])
  68.                 {
  69.                     queue.Enqueue(new Cell() {X = x, Level = level + 1, Y = y - 1});
  70.                     visited[x, y - 1] = true;
  71.                 }
  72.  
  73.                 if (y + 1 < matrix.GetLength(1) && matrix[x, y + 1] != "X" && !visited[x, y + 1])
  74.                 {
  75.                     queue.Enqueue(new Cell() {X = x, Level = level + 1, Y = y + 1});
  76.                     visited[x, y + 1] = true;
  77.                 }
  78.             }
  79.  
  80.             for (int i = 0; i < matrix.GetLength(0); i++)
  81.             {
  82.                 for (int j = 0; j < matrix.GetLength(1); j++)
  83.                 {
  84.                     if (matrix[i, j] == "0")
  85.                     {
  86.                         matrix[i, j] = "u";
  87.                     }
  88.                 }
  89.             }
  90.         }
  91.  
  92.         private static Cell FindStartingCell()
  93.         {
  94.             var startingCell = new Cell();
  95.  
  96.             for (int i = 0; i < matrix.GetLength(1); i++)
  97.             {
  98.                 for (int j = 0; j < matrix.GetLength(0); j++)
  99.                 {
  100.                     if (matrix[j, i] == "*")
  101.                     {
  102.                         startingCell.X = j;
  103.                         startingCell.Y = i;
  104.  
  105.                         return startingCell;
  106.                     }
  107.                 }
  108.             }
  109.  
  110.             throw new ArgumentException("No such cell found!");
  111.         }
  112.     }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement