Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace Portal
- {
- public class Portal
- {
- public static char[][] matrix;
- public static void Main()
- {
- int numberOfRows = int.Parse(Console.ReadLine());
- matrix = new char[numberOfRows][];
- int[] currPlace = new int[2];
- int[] end = new int[2];
- for (int currRow = 0; currRow < numberOfRows; currRow++)
- {
- string inputLine = Console.ReadLine().ToLower();
- matrix[currRow] = new char[inputLine.Length];
- for (int col = 0; col < inputLine.Length; col++)
- {
- char symbol = inputLine[col];
- matrix[currRow][col] = symbol;
- if (symbol == 's')
- {
- currPlace[0] = currRow;
- currPlace[1] = col;
- }
- else if (symbol == 'e')
- {
- end[0] = currRow;
- end[1] = col;
- }
- }
- }
- string moves = Console.ReadLine().ToLower();
- string result = string.Empty;
- int countOfTurns = 0;
- bool gotToTheEnd = false;
- for (int currMove = 0; currMove < moves.Length; currMove++)
- {
- char move = moves[currMove];
- if (move == 'd')
- {
- gotToTheEnd = MoveDownAndCheckIfItIsEnd(currPlace);
- }
- else if (move == 'u')
- {
- gotToTheEnd = MoveUpAndCheckIfItIsEnd(currPlace);
- }
- else if (move == 'r')
- {
- gotToTheEnd = MoveRightAndCheckIfItIsEnd(currPlace);
- }
- else if (move == 'l')
- {
- gotToTheEnd = MoveLeftAndCheckIfItIsEnd(currPlace);
- }
- countOfTurns++;
- if (gotToTheEnd)
- {
- result = $"Experiment successful. {countOfTurns} turns required.";
- break;
- }
- }
- if (!gotToTheEnd)
- {
- result = $"Robot stuck at {currPlace[0]} {currPlace[1]}. Experiment failed.";
- }
- Console.WriteLine(result);
- }
- public static bool MoveLeftAndCheckIfItIsEnd(int[] currPlace)
- {
- bool gotToTheEnd = false;
- bool madeTheMove = false;
- int row = currPlace[0];
- int nextCol = currPlace[1] - 1;
- while (!madeTheMove)
- {
- if (nextCol == -1)
- {
- nextCol = matrix[row].Length - 1;
- }
- else
- {
- if (matrix[row][nextCol] == 'e')
- {
- gotToTheEnd = true;
- break;
- }
- currPlace[1] = nextCol;
- madeTheMove = true;
- }
- }
- return gotToTheEnd;
- }
- public static bool MoveRightAndCheckIfItIsEnd(int[] currPlace)
- {
- bool gotToTheEnd = false;
- bool madeTheMove = false;
- int row = currPlace[0];
- int nextCol = currPlace[1] + 1;
- while (!madeTheMove)
- {
- if (matrix[row].Length <= nextCol)
- {
- nextCol = 0;
- }
- else
- {
- if (matrix[row][nextCol] == 'e')
- {
- gotToTheEnd = true;
- break;
- }
- currPlace[1] = nextCol;
- madeTheMove = true;
- }
- }
- return gotToTheEnd;
- }
- public static bool MoveUpAndCheckIfItIsEnd(int[] currPlace)
- {
- bool gotToTheEnd = false;
- int nextRow = currPlace[0] - 1;
- int col = currPlace[1];
- bool madeTheMove = false;
- while (!madeTheMove)
- {
- if (nextRow == -1)
- {
- nextRow = matrix.Length - 1;
- }
- else if (matrix[nextRow].Length <= col)
- {
- nextRow--;
- }
- else
- {
- if (matrix[nextRow][col] == 'e')
- {
- gotToTheEnd = true;
- break;
- }
- currPlace[0] = nextRow;
- madeTheMove = true;
- }
- }
- return gotToTheEnd;
- }
- public static bool MoveDownAndCheckIfItIsEnd(int[] currPlace)
- {
- bool gotToTheEnd = false;
- bool madeTheMove = false;
- int nextRow = currPlace[0] + 1;
- int col = currPlace[1];
- while (!madeTheMove)
- {
- if (matrix.Length == nextRow)
- {
- nextRow = 0;
- }
- else if (matrix[nextRow].Length <= col)
- {
- nextRow++;
- }
- else
- {
- if (matrix[nextRow][col] == 'e')
- {
- gotToTheEnd = true;
- break;
- }
- madeTheMove = true;
- currPlace[0] = nextRow;
- }
- }
- return gotToTheEnd;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement