Advertisement
Adrian_Apostolov

3DLabytinrh

Dec 2nd, 2015
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.58 KB | None | 0 0
  1. namespace _3DLabyrinth
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using System.Linq;
  6.     using System.Text;
  7.     using System.Threading.Tasks;
  8.  
  9.     class Program
  10.     {
  11.         static void Main()
  12.         {
  13.             var startPositionParts = Console.ReadLine().Split(' ');
  14.             var startPositionX = int.Parse(startPositionParts[0]);
  15.             var startPositionY = int.Parse(startPositionParts[1]);
  16.             var startPositionZ = int.Parse(startPositionParts[2]);
  17.  
  18.             var startCell = new Cell(startPositionX, startPositionY, startPositionZ, 0);
  19.  
  20.             var dimensionsParts = Console.ReadLine().Split();
  21.             var depth = int.Parse(dimensionsParts[0]);
  22.             var rows = int.Parse(dimensionsParts[1]);
  23.             var columns = int.Parse(dimensionsParts[2]);
  24.  
  25.             var matrix = new int[depth, rows, columns];
  26.             var used = new bool[depth, rows, columns];
  27.  
  28.             used[startCell.Depth, startCell.Row, startCell.Column] = true;
  29.  
  30.             for (int i = 0; i < depth; i++)
  31.             {
  32.                 for (int j = 0; j < rows; j++)
  33.                 {
  34.                     var line = Console.ReadLine();
  35.  
  36.                     for (int d = 0; d < columns; d++)
  37.                     {
  38.                         matrix[i, j, d] = line[d];
  39.  
  40.                         if (matrix[i, j, d] == '#')
  41.                         {
  42.                             used[i, j, d] = true;
  43.                         }
  44.                     }
  45.                 }
  46.             }
  47.  
  48.             var queue = new Queue<Cell>();
  49.             queue.Enqueue(startCell);
  50.  
  51.             while (queue.Count > 0)
  52.             {
  53.                 var currentCell = queue.Dequeue();
  54.  
  55.                 if (currentCell.Column > 0)
  56.                 {
  57.                     var newCell = new Cell(currentCell.Depth, currentCell.Row, currentCell.Column - 1, currentCell.StepCount + 1);
  58.  
  59.                     if (used[newCell.Depth, newCell.Row, newCell.Column] == false)
  60.                     {
  61.                         queue.Enqueue(newCell);
  62.                         used[newCell.Depth, newCell.Row, newCell.Column] = true;
  63.                     }
  64.                 }
  65.  
  66.                 if (currentCell.Column < columns - 1)
  67.                 {
  68.                     var newCell = new Cell(currentCell.Depth, currentCell.Row, currentCell.Column + 1, currentCell.StepCount + 1);
  69.  
  70.                     if (used[newCell.Depth, newCell.Row, newCell.Column] == false)
  71.                     {
  72.                         queue.Enqueue(newCell);
  73.                         used[newCell.Depth, newCell.Row, newCell.Column] = true;
  74.                     }
  75.                 }
  76.  
  77.                 if (currentCell.Row > 0)
  78.                 {
  79.                     var newCell = new Cell(currentCell.Depth, currentCell.Row - 1, currentCell.Column, currentCell.StepCount + 1);
  80.  
  81.                     if (used[newCell.Depth, newCell.Row, newCell.Column] == false)
  82.                     {
  83.                         queue.Enqueue(newCell);
  84.                         used[newCell.Depth, newCell.Row, newCell.Column] = true;
  85.                     }
  86.                 }
  87.  
  88.                 if (currentCell.Row < rows - 1)
  89.                 {
  90.                     var newCell = new Cell(currentCell.Depth, currentCell.Row + 1, currentCell.Column, currentCell.StepCount + 1);
  91.  
  92.                     if (used[newCell.Depth, newCell.Row, newCell.Column] == false)
  93.                     {
  94.                         queue.Enqueue(newCell);
  95.                         used[newCell.Depth, newCell.Row, newCell.Column] = true;
  96.                     }
  97.                 }
  98.  
  99.                 if (matrix[currentCell.Depth, currentCell.Row, currentCell.Column] == 'U')
  100.                 {
  101.                     if (currentCell.Depth == depth - 1)
  102.                     {
  103.                         Console.WriteLine(currentCell.StepCount + 1);
  104.                         Environment.Exit(0);
  105.                     }
  106.                     else
  107.                     {
  108.                         var newCell = new Cell(currentCell.Depth + 1, currentCell.Row, currentCell.Column, currentCell.StepCount + 1);
  109.  
  110.                         if (used[newCell.Depth, newCell.Row, newCell.Column] == false)
  111.                         {
  112.                             queue.Enqueue(newCell);
  113.                             used[newCell.Depth, newCell.Row, newCell.Column] = true;
  114.                         }
  115.                     }
  116.                 }
  117.  
  118.                 if (matrix[currentCell.Depth, currentCell.Row, currentCell.Column] == 'D')
  119.                 {
  120.                     if (currentCell.Depth == 0)
  121.                     {
  122.                         Console.WriteLine(currentCell.StepCount + 1);
  123.                         Environment.Exit(0);
  124.                     }
  125.                     else
  126.                     {
  127.                         var newCell = new Cell(currentCell.Depth - 1, currentCell.Row, currentCell.Column, currentCell.StepCount + 1);
  128.                         newCell.StepCount++;
  129.  
  130.                         if (used[newCell.Depth, newCell.Row, newCell.Column] == false)
  131.                         {
  132.                             queue.Enqueue(newCell);
  133.                             used[newCell.Depth, newCell.Row, newCell.Column] = true;
  134.                         }
  135.                     }
  136.                 }
  137.             }
  138.         }
  139.     }
  140.  
  141.     class Cell
  142.     {
  143.         public Cell(int depth, int row, int column, int stepCount)
  144.         {
  145.             this.Depth = depth;
  146.             this.Row = row;
  147.             this.Column = column;
  148.             this.StepCount = stepCount;
  149.         }
  150.  
  151.         public int Depth { get; set; }
  152.  
  153.         public int Row { get; set; }
  154.  
  155.         public int Column { get; set; }
  156.  
  157.         public int StepCount { get; set; }
  158.     }
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement