Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace _3DLabyrinth
- {
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- class Program
- {
- static void Main()
- {
- var startPositionParts = Console.ReadLine().Split(' ');
- var startPositionX = int.Parse(startPositionParts[0]);
- var startPositionY = int.Parse(startPositionParts[1]);
- var startPositionZ = int.Parse(startPositionParts[2]);
- var startCell = new Cell(startPositionX, startPositionY, startPositionZ, 0);
- var dimensionsParts = Console.ReadLine().Split();
- var depth = int.Parse(dimensionsParts[0]);
- var rows = int.Parse(dimensionsParts[1]);
- var columns = int.Parse(dimensionsParts[2]);
- var matrix = new int[depth, rows, columns];
- var used = new bool[depth, rows, columns];
- used[startCell.Depth, startCell.Row, startCell.Column] = true;
- for (int i = 0; i < depth; i++)
- {
- for (int j = 0; j < rows; j++)
- {
- var line = Console.ReadLine();
- for (int d = 0; d < columns; d++)
- {
- matrix[i, j, d] = line[d];
- if (matrix[i, j, d] == '#')
- {
- used[i, j, d] = true;
- }
- }
- }
- }
- var queue = new Queue<Cell>();
- queue.Enqueue(startCell);
- while (queue.Count > 0)
- {
- var currentCell = queue.Dequeue();
- if (currentCell.Column > 0)
- {
- var newCell = new Cell(currentCell.Depth, currentCell.Row, currentCell.Column - 1, currentCell.StepCount + 1);
- if (used[newCell.Depth, newCell.Row, newCell.Column] == false)
- {
- queue.Enqueue(newCell);
- used[newCell.Depth, newCell.Row, newCell.Column] = true;
- }
- }
- if (currentCell.Column < columns - 1)
- {
- var newCell = new Cell(currentCell.Depth, currentCell.Row, currentCell.Column + 1, currentCell.StepCount + 1);
- if (used[newCell.Depth, newCell.Row, newCell.Column] == false)
- {
- queue.Enqueue(newCell);
- used[newCell.Depth, newCell.Row, newCell.Column] = true;
- }
- }
- if (currentCell.Row > 0)
- {
- var newCell = new Cell(currentCell.Depth, currentCell.Row - 1, currentCell.Column, currentCell.StepCount + 1);
- if (used[newCell.Depth, newCell.Row, newCell.Column] == false)
- {
- queue.Enqueue(newCell);
- used[newCell.Depth, newCell.Row, newCell.Column] = true;
- }
- }
- if (currentCell.Row < rows - 1)
- {
- var newCell = new Cell(currentCell.Depth, currentCell.Row + 1, currentCell.Column, currentCell.StepCount + 1);
- if (used[newCell.Depth, newCell.Row, newCell.Column] == false)
- {
- queue.Enqueue(newCell);
- used[newCell.Depth, newCell.Row, newCell.Column] = true;
- }
- }
- if (matrix[currentCell.Depth, currentCell.Row, currentCell.Column] == 'U')
- {
- if (currentCell.Depth == depth - 1)
- {
- Console.WriteLine(currentCell.StepCount + 1);
- Environment.Exit(0);
- }
- else
- {
- var newCell = new Cell(currentCell.Depth + 1, currentCell.Row, currentCell.Column, currentCell.StepCount + 1);
- if (used[newCell.Depth, newCell.Row, newCell.Column] == false)
- {
- queue.Enqueue(newCell);
- used[newCell.Depth, newCell.Row, newCell.Column] = true;
- }
- }
- }
- if (matrix[currentCell.Depth, currentCell.Row, currentCell.Column] == 'D')
- {
- if (currentCell.Depth == 0)
- {
- Console.WriteLine(currentCell.StepCount + 1);
- Environment.Exit(0);
- }
- else
- {
- var newCell = new Cell(currentCell.Depth - 1, currentCell.Row, currentCell.Column, currentCell.StepCount + 1);
- newCell.StepCount++;
- if (used[newCell.Depth, newCell.Row, newCell.Column] == false)
- {
- queue.Enqueue(newCell);
- used[newCell.Depth, newCell.Row, newCell.Column] = true;
- }
- }
- }
- }
- }
- }
- class Cell
- {
- public Cell(int depth, int row, int column, int stepCount)
- {
- this.Depth = depth;
- this.Row = row;
- this.Column = column;
- this.StepCount = stepCount;
- }
- public int Depth { get; set; }
- public int Row { get; set; }
- public int Column { get; set; }
- public int StepCount { get; set; }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement