Advertisement
Guest User

Untitled

a guest
Jun 6th, 2018
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.43 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4. using System.Text.RegularExpressions;
  5.  
  6. namespace Portal
  7. {
  8.     class Portal
  9.     {
  10.         struct EndPoint
  11.         {
  12.             public int Row;
  13.             public int Column;
  14.  
  15.             public EndPoint(int row = 0, int column = 0)
  16.             {
  17.                 Row = row;
  18.                 Column = column;
  19.             }
  20.         };
  21.  
  22.         static void Main(string[] args)
  23.         {
  24.             int matrixSize = int.Parse(Console.ReadLine());
  25.             List<List<char>> matrix = new List<List<char>>();
  26.             string patternMatrix = @"[SEO]";
  27.             Regex regexMatrix = new Regex(patternMatrix);
  28.             string patternLetter = @"[DURL]";
  29.             Regex regexLetter = new Regex(patternLetter);
  30.             bool isPath = true;
  31.  
  32.             int startRow = 0;
  33.             int startColumn = 0;
  34.  
  35.             List<EndPoint> endPoints = new List<EndPoint>();
  36.             //int endRow = -1;
  37.             //int endColumn = -1;
  38.  
  39.             for (int i = 0; i < matrixSize; i++)
  40.             {
  41.                 matrix.Add(new List<char>());
  42.                 matrix[i] = Console.ReadLine().ToList();
  43.                 if (matrix[i].Any(x => x == 'S'))
  44.                 {
  45.                     startRow = i;
  46.                     startColumn = matrix[i].IndexOf('S');
  47.                 }
  48.                 if (matrix[i].Any(x => x == 'E'))
  49.                 {
  50.                     endPoints.Add(new EndPoint(i, matrix[i].IndexOf('E')));
  51.                     //endRow = i;
  52.                     //endColumn = matrix[i].IndexOf('E');
  53.                 }
  54.             }
  55.  
  56.             if (endPoints.Count == 0)
  57.             {
  58.                 Console.WriteLine($"Robot stuck at {startRow} {startColumn}. Experiment failed.");
  59.                 return;
  60.             }
  61.             else
  62.             {
  63.                 string directions = Console.ReadLine();
  64.                 int turns = 0;
  65.  
  66.                 foreach (char letter in directions)
  67.                 {
  68.                     if (regexLetter.IsMatch(letter.ToString()))
  69.                     {
  70.                         switch (letter)
  71.                         {
  72.                             case 'R':
  73.                                 while (true)
  74.                                 {
  75.                                     startColumn = (startColumn + 1) % matrix[startRow].Count;
  76.                                     if (regexMatrix.IsMatch(matrix[startRow][startColumn].ToString()))
  77.                                     {
  78.                                         break;
  79.                                     }
  80.                                     else
  81.                                     {
  82.                                         isPath = false;
  83.                                         break;
  84.                                     }
  85.                                 }
  86.                                 break;
  87.  
  88.                             case 'L':
  89.                                 while (true)
  90.                                 {
  91.                                     startColumn = (startColumn + matrix[startRow].Count - 1) % matrix[startRow].Count;
  92.                                     if (regexMatrix.IsMatch(matrix[startRow][startColumn].ToString()))
  93.                                     {
  94.                                         break;
  95.                                     }
  96.                                     else
  97.                                     {
  98.                                         isPath = false;
  99.                                         break;
  100.                                     }
  101.                                 }
  102.                                 break;
  103.  
  104.                             case 'D':
  105.                                 while (true)
  106.                                 {
  107.                                     startRow = (startRow + 1) % matrixSize;
  108.                                     if (startColumn > matrix[startRow].Count - 1)
  109.                                     {
  110.                                         continue;
  111.                                     }
  112.                                     else if (regexMatrix.IsMatch(matrix[startRow][startColumn].ToString()))
  113.                                     {
  114.                                         break;
  115.                                     }
  116.                                     else
  117.                                     {
  118.                                         isPath = false;
  119.                                         break;
  120.                                     }
  121.                                 }
  122.                                 break;
  123.  
  124.                             case 'U':
  125.                                 while (true)
  126.                                 {
  127.                                     startRow = (startRow + matrixSize - 1) % matrixSize;
  128.  
  129.                                     if (startColumn > matrix[startRow].Count - 1)
  130.                                     {
  131.                                         continue;
  132.                                     }
  133.                                     else if (regexMatrix.IsMatch(matrix[startRow][startColumn].ToString()))
  134.                                     {
  135.                                         break;
  136.                                     }
  137.                                     else
  138.                                     {
  139.                                         isPath = false;
  140.                                         break;
  141.                                     }
  142.                                 }
  143.                                 break;
  144.                         }
  145.  
  146.  
  147.                         //Console.WriteLine();
  148.                         //Console.WriteLine(letter);
  149.                         //for (int i = 0; i < matrix.Count; i++)
  150.                         //{
  151.                         //    for (int j = 0; j < matrix[i].Count; j++)
  152.                         //    {
  153.                         //        if (i == startRow && j == startColumn)
  154.                         //        {
  155.                         //            Console.Write("#");
  156.                         //        }
  157.                         //        else
  158.                         //        {
  159.                         //            Console.Write(matrix[i][j]);
  160.                         //        }
  161.                         //    }
  162.                         //    Console.WriteLine();
  163.                         //}
  164.  
  165.  
  166.                         turns++;
  167.  
  168.                         if (!isPath)
  169.                         {
  170.                             Console.WriteLine($"Robot stuck at {startRow} {startColumn}. Experiment failed.");
  171.                             return;
  172.                         }
  173.  
  174.  
  175.                         foreach (var e in endPoints)
  176.                         {
  177.                             if (startRow == e.Row && startColumn == e.Column)
  178.                             {
  179.                                 Console.WriteLine($"Experiment successful. {turns} turns required.");
  180.                                 return;
  181.                             }
  182.                         }
  183.  
  184.                         //if (startRow == endRow && startColumn == endColumn)
  185.                         //{
  186.                         //    Console.WriteLine($"Experiment successful. {turns} turns required.");
  187.                         //    return;
  188.                         //}
  189.                     }
  190.                 }
  191.  
  192.                 Console.WriteLine($"Robot stuck at {startRow} {startColumn}. Experiment failed.");
  193.             }
  194.         }
  195.     }
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement