Advertisement
Guest User

Untitled

a guest
Feb 20th, 2016
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.40 KB | None | 0 0
  1. namespace DistanceLabyrinth
  2. {
  3.     using System;
  4.     using System.Collections;
  5.     using System.Collections.Generic;
  6.     using System.Linq;
  7.  
  8.     class DistanceLabyrinth
  9.     {
  10.         private static string unreachableCellSymbol = "u";
  11.         private static string startingCellSymbol = "*";
  12.         private static int startingIndex = 1;
  13.         private static int childrenCounter = 0;
  14.         static void Main(string[] args)
  15.         {
  16.             string[,] matrix = new string[,]
  17.             {
  18.                 { "0", "0", "0", "x", "0", "x" },
  19.                 { "0", "x", "0", "x", "0", "x" },
  20.                 { "0", "*", "x", "0", "x", "0" },
  21.                 { "0", "x", "0", "0", "0", "0" },
  22.                 { "0", "0", "0", "x", "x", "0" },
  23.                 { "0", "0", "0", "x", "0", "x" }
  24.             };
  25.  
  26.             CalculateDistances(matrix);
  27.             MarkUnreachableCells(matrix);
  28.             PrintMatrix(matrix);
  29.         }
  30.  
  31.         private static void CalculateDistances(string[,] matrix)
  32.         {
  33.             Queue<Tuple<int, int>> cellsQueue = new Queue<Tuple<int, int>>();
  34.             Tuple<int, int> startingCell = FindStartingCell(matrix);
  35.             cellsQueue.Enqueue(startingCell);
  36.             int counter = startingIndex;
  37.             childrenCounter = 1;
  38.             while (cellsQueue.Count > 0)
  39.             {
  40.                 int tempChildrenCounter = 0;
  41.                 while (childrenCounter > 0)
  42.                 {
  43.                     Tuple<int, int> currentCell = cellsQueue.Dequeue();
  44.                     childrenCounter--;
  45.                     EnqueueNearbyCells(cellsQueue, matrix, currentCell, counter, ref tempChildrenCounter);
  46.                 }
  47.  
  48.                 counter++;
  49.                 childrenCounter = tempChildrenCounter;
  50.             }
  51.         }
  52.  
  53.         private static void EnqueueNearbyCells(Queue<Tuple<int, int>> cellsQueue, string[,] matrix, Tuple<int, int> currentCell, int counter, ref int tempChildrenCounter)
  54.         {
  55.             AddToQueue(cellsQueue, matrix, currentCell.Item1 - 1, currentCell.Item2, counter, ref tempChildrenCounter);
  56.             AddToQueue(cellsQueue, matrix, currentCell.Item1 + 1, currentCell.Item2, counter, ref tempChildrenCounter);
  57.             AddToQueue(cellsQueue, matrix, currentCell.Item1, currentCell.Item2 - 1, counter, ref tempChildrenCounter);
  58.             AddToQueue(cellsQueue, matrix, currentCell.Item1, currentCell.Item2 + 1, counter, ref tempChildrenCounter);
  59.         }
  60.  
  61.         private static void AddToQueue(Queue<Tuple<int, int>> cellsQueue, string[,] matrix, int x, int y, int counter, ref int tempChildrenCounter)
  62.         {
  63.             if (x < 0 || y < 0 || x >= matrix.GetLength(0) || y >= matrix.GetLength(1))
  64.             {
  65.                 return;
  66.             }
  67.  
  68.             if (matrix[x, y] != "0")
  69.             {
  70.                 return;
  71.             }
  72.  
  73.             matrix[x, y] = counter.ToString();
  74.             tempChildrenCounter++;
  75.             cellsQueue.Enqueue(new Tuple<int, int>(x, y));
  76.         }
  77.  
  78.         private static Tuple<int, int> FindStartingCell(string[,] matrix)
  79.         {
  80.             int startX = 0;
  81.             int startY = 0;
  82.             for (int i = 0; i < matrix.GetLength(0); i++)
  83.             {
  84.                 for (int j = 0; j < matrix.GetLength(1); j++)
  85.                 {
  86.                     if (matrix[i, j] == startingCellSymbol)
  87.                     {
  88.                         startX = i;
  89.                         startY = j;
  90.                         break;
  91.                     }
  92.                 }
  93.             }
  94.  
  95.             return new Tuple<int, int>(startX, startY);
  96.         }
  97.  
  98.         private static void MarkUnreachableCells(string[,] matrix)
  99.         {
  100.             for (int i = 0; i < matrix.GetLength(0); i++)
  101.             {
  102.                 for (int j = 0; j < matrix.GetLength(1); j++)
  103.                 {
  104.                     if (matrix[i, j] == "0")
  105.                     {
  106.                         matrix[i, j] = unreachableCellSymbol;
  107.                     }
  108.                 }
  109.             }
  110.         }
  111.  
  112.         private static void PrintMatrix<T>(T[,] matrix)
  113.         {
  114.             for (int i = 0; i < matrix.GetLength(0); i++)
  115.             {
  116.                 for (int j = 0; j < matrix.GetLength(1); j++)
  117.                 {
  118.                     Console.Write("{0,2}", matrix[i, j]);
  119.                 }
  120.                 Console.WriteLine();
  121.             }
  122.         }
  123.     }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement