Advertisement
simonradev

Portal

Feb 19th, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.86 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Portal
  4. {
  5.     public class Portal
  6.     {
  7.         public static char[][] matrix;
  8.  
  9.         public static void Main()
  10.         {
  11.             int numberOfRows = int.Parse(Console.ReadLine());
  12.  
  13.             matrix = new char[numberOfRows][];
  14.             int[] currPlace = new int[2];
  15.             int[] end = new int[2];
  16.             for (int currRow = 0; currRow < numberOfRows; currRow++)
  17.             {
  18.                 string inputLine = Console.ReadLine().ToLower();
  19.  
  20.                 matrix[currRow] = new char[inputLine.Length];
  21.                 for (int col = 0; col < inputLine.Length; col++)
  22.                 {
  23.                     char symbol = inputLine[col];
  24.  
  25.                     matrix[currRow][col] = symbol;
  26.  
  27.                     if (symbol == 's')
  28.                     {
  29.                         currPlace[0] = currRow;
  30.                         currPlace[1] = col;
  31.                     }
  32.                     else if (symbol == 'e')
  33.                     {
  34.                         end[0] = currRow;
  35.                         end[1] = col;
  36.                     }
  37.                 }
  38.             }
  39.  
  40.             string moves = Console.ReadLine().ToLower();
  41.             string result = string.Empty;
  42.             int countOfTurns = 0;
  43.             bool gotToTheEnd = false;
  44.             for (int currMove = 0; currMove < moves.Length; currMove++)
  45.             {
  46.                 char move = moves[currMove];
  47.  
  48.                 if (move == 'd')
  49.                 {
  50.                     gotToTheEnd = MoveDownAndCheckIfItIsEnd(currPlace);
  51.                 }
  52.                 else if (move == 'u')
  53.                 {
  54.                     gotToTheEnd = MoveUpAndCheckIfItIsEnd(currPlace);
  55.                 }
  56.                 else if (move == 'r')
  57.                 {
  58.                     gotToTheEnd = MoveRightAndCheckIfItIsEnd(currPlace);
  59.                 }
  60.                 else if (move == 'l')
  61.                 {
  62.                     gotToTheEnd = MoveLeftAndCheckIfItIsEnd(currPlace);
  63.                 }
  64.  
  65.                 countOfTurns++;
  66.  
  67.                 if (gotToTheEnd)
  68.                 {
  69.                     result = $"Experiment successful. {countOfTurns} turns required.";
  70.  
  71.                     break;
  72.                 }
  73.             }
  74.  
  75.             if (!gotToTheEnd)
  76.             {
  77.                 result = $"Robot stuck at {currPlace[0]} {currPlace[1]}. Experiment failed.";
  78.             }
  79.  
  80.             Console.WriteLine(result);
  81.         }
  82.  
  83.         public static bool MoveLeftAndCheckIfItIsEnd(int[] currPlace)
  84.         {
  85.             bool gotToTheEnd = false;
  86.  
  87.             bool madeTheMove = false;
  88.             int row = currPlace[0];
  89.             int nextCol = currPlace[1] - 1;
  90.             while (!madeTheMove)
  91.             {
  92.                 if (nextCol == -1)
  93.                 {
  94.                     nextCol = matrix[row].Length - 1;
  95.                 }
  96.                 else
  97.                 {
  98.                     if (matrix[row][nextCol] == 'e')
  99.                     {
  100.                         gotToTheEnd = true;
  101.                         break;
  102.                     }
  103.  
  104.                     currPlace[1] = nextCol;
  105.  
  106.                     madeTheMove = true;
  107.                 }
  108.             }
  109.  
  110.             return gotToTheEnd;
  111.         }
  112.  
  113.         public static bool MoveRightAndCheckIfItIsEnd(int[] currPlace)
  114.         {
  115.             bool gotToTheEnd = false;
  116.  
  117.             bool madeTheMove = false;
  118.             int row = currPlace[0];
  119.             int nextCol = currPlace[1] + 1;
  120.             while (!madeTheMove)
  121.             {
  122.                 if (matrix[row].Length <= nextCol)
  123.                 {
  124.                     nextCol = 0;
  125.                 }
  126.                 else
  127.                 {
  128.                     if (matrix[row][nextCol] == 'e')
  129.                     {
  130.                         gotToTheEnd = true;
  131.                         break;
  132.                     }
  133.  
  134.                     currPlace[1] = nextCol;
  135.  
  136.                     madeTheMove = true;
  137.                 }
  138.             }
  139.  
  140.             return gotToTheEnd;
  141.         }
  142.  
  143.         public static bool MoveUpAndCheckIfItIsEnd(int[] currPlace)
  144.         {
  145.             bool gotToTheEnd = false;
  146.  
  147.             int nextRow = currPlace[0] - 1;
  148.             int col = currPlace[1];
  149.             bool madeTheMove = false;
  150.             while (!madeTheMove)
  151.             {
  152.                 if (nextRow == -1)
  153.                 {
  154.                     nextRow = matrix.Length - 1;
  155.                 }
  156.                 else if (matrix[nextRow].Length <= col)
  157.                 {
  158.                     nextRow--;
  159.                 }
  160.                 else
  161.                 {
  162.                     if (matrix[nextRow][col] == 'e')
  163.                     {
  164.                         gotToTheEnd = true;
  165.                         break;
  166.                     }
  167.  
  168.                     currPlace[0] = nextRow;
  169.  
  170.                     madeTheMove = true;
  171.                 }
  172.             }
  173.  
  174.             return gotToTheEnd;
  175.         }
  176.  
  177.         public static bool MoveDownAndCheckIfItIsEnd(int[] currPlace)
  178.         {
  179.             bool gotToTheEnd = false;
  180.  
  181.             bool madeTheMove = false;
  182.             int nextRow = currPlace[0] + 1;
  183.             int col = currPlace[1];
  184.             while (!madeTheMove)
  185.             {
  186.                 if (matrix.Length == nextRow)
  187.                 {
  188.                     nextRow = 0;
  189.                 }
  190.                 else if (matrix[nextRow].Length <= col)
  191.                 {
  192.                     nextRow++;
  193.                 }
  194.                 else
  195.                 {
  196.                     if (matrix[nextRow][col] == 'e')
  197.                     {
  198.                         gotToTheEnd = true;
  199.                         break;
  200.                     }
  201.  
  202.                     madeTheMove = true;
  203.  
  204.                     currPlace[0] = nextRow;
  205.                 }
  206.             }
  207.  
  208.             return gotToTheEnd;
  209.         }
  210.     }
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement