Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- using System.Collections.Generic;
- using System.Text.RegularExpressions;
- namespace Portal
- {
- class Portal
- {
- struct EndPoint
- {
- public int Row;
- public int Column;
- public EndPoint(int row = 0, int column = 0)
- {
- Row = row;
- Column = column;
- }
- };
- static void Main(string[] args)
- {
- int matrixSize = int.Parse(Console.ReadLine());
- List<List<char>> matrix = new List<List<char>>();
- string patternMatrix = @"[SEO]";
- Regex regexMatrix = new Regex(patternMatrix);
- string patternLetter = @"[DURL]";
- Regex regexLetter = new Regex(patternLetter);
- bool isPath = true;
- int startRow = 0;
- int startColumn = 0;
- List<EndPoint> endPoints = new List<EndPoint>();
- //int endRow = -1;
- //int endColumn = -1;
- for (int i = 0; i < matrixSize; i++)
- {
- matrix.Add(new List<char>());
- matrix[i] = Console.ReadLine().ToList();
- if (matrix[i].Any(x => x == 'S'))
- {
- startRow = i;
- startColumn = matrix[i].IndexOf('S');
- }
- if (matrix[i].Any(x => x == 'E'))
- {
- endPoints.Add(new EndPoint(i, matrix[i].IndexOf('E')));
- //endRow = i;
- //endColumn = matrix[i].IndexOf('E');
- }
- }
- if (endPoints.Count == 0)
- {
- Console.WriteLine($"Robot stuck at {startRow} {startColumn}. Experiment failed.");
- return;
- }
- else
- {
- string directions = Console.ReadLine();
- int turns = 0;
- foreach (char letter in directions)
- {
- if (regexLetter.IsMatch(letter.ToString()))
- {
- switch (letter)
- {
- case 'R':
- while (true)
- {
- startColumn = (startColumn + 1) % matrix[startRow].Count;
- if (regexMatrix.IsMatch(matrix[startRow][startColumn].ToString()))
- {
- break;
- }
- else
- {
- isPath = false;
- break;
- }
- }
- break;
- case 'L':
- while (true)
- {
- startColumn = (startColumn + matrix[startRow].Count - 1) % matrix[startRow].Count;
- if (regexMatrix.IsMatch(matrix[startRow][startColumn].ToString()))
- {
- break;
- }
- else
- {
- isPath = false;
- break;
- }
- }
- break;
- case 'D':
- while (true)
- {
- startRow = (startRow + 1) % matrixSize;
- if (startColumn > matrix[startRow].Count - 1)
- {
- continue;
- }
- else if (regexMatrix.IsMatch(matrix[startRow][startColumn].ToString()))
- {
- break;
- }
- else
- {
- isPath = false;
- break;
- }
- }
- break;
- case 'U':
- while (true)
- {
- startRow = (startRow + matrixSize - 1) % matrixSize;
- if (startColumn > matrix[startRow].Count - 1)
- {
- continue;
- }
- else if (regexMatrix.IsMatch(matrix[startRow][startColumn].ToString()))
- {
- break;
- }
- else
- {
- isPath = false;
- break;
- }
- }
- break;
- }
- //Console.WriteLine();
- //Console.WriteLine(letter);
- //for (int i = 0; i < matrix.Count; i++)
- //{
- // for (int j = 0; j < matrix[i].Count; j++)
- // {
- // if (i == startRow && j == startColumn)
- // {
- // Console.Write("#");
- // }
- // else
- // {
- // Console.Write(matrix[i][j]);
- // }
- // }
- // Console.WriteLine();
- //}
- turns++;
- if (!isPath)
- {
- Console.WriteLine($"Robot stuck at {startRow} {startColumn}. Experiment failed.");
- return;
- }
- foreach (var e in endPoints)
- {
- if (startRow == e.Row && startColumn == e.Column)
- {
- Console.WriteLine($"Experiment successful. {turns} turns required.");
- return;
- }
- }
- //if (startRow == endRow && startColumn == endColumn)
- //{
- // Console.WriteLine($"Experiment successful. {turns} turns required.");
- // return;
- //}
- }
- }
- Console.WriteLine($"Robot stuck at {startRow} {startColumn}. Experiment failed.");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement