Advertisement
SimeonSheytanov

Portal

Sep 13th, 2016
484
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.74 KB | None | 0 0
  1. namespace Portal
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using System.Linq;
  6.  
  7.     public class Portal
  8.     {
  9.         public static void Main(string[] args)
  10.         {
  11.             int rows = int.Parse(Console.ReadLine());
  12.             List<List<char>> matrix = ReadMatrix(rows);
  13.             char[] path = Console.ReadLine().ToCharArray();
  14.  
  15.             FindExit(matrix, path);
  16.         }
  17.  
  18.         private static void FindExit(List<List<char>> matrix, char[] path)
  19.         {
  20.             int turns = 0;
  21.             int playerRow = matrix.IndexOf(matrix.Single(row => row.Contains('S')));
  22.             int playerCol = matrix.Single(row => row.Contains('S')).IndexOf('S');
  23.            
  24.             Position player = new Position(playerRow, playerCol);
  25.  
  26.             foreach (char direction in path)
  27.             {
  28.                 int dir = DirectionGetter(direction);
  29.  
  30.                 Move(dir, matrix, player);
  31.  
  32.                 turns++;
  33.  
  34.                 if (matrix[player.Row][player.Col] != 'E') continue;
  35.  
  36.                 Console.WriteLine("Experiment successful. {0} turns required.", turns);
  37.                 return;
  38.             }
  39.  
  40.             Console.WriteLine("Robot stuck at {0} {1}. Experiment failed.", player.Row, player.Col);
  41.         }
  42.  
  43.         private static void Move(int dir, List<List<char>> matrix, Position player)
  44.         {
  45.             switch (dir)
  46.             {
  47.                 case 2:
  48.                     MoveVertical(1, matrix, player);
  49.                     break;
  50.                 case -2:
  51.                     MoveVertical(-1, matrix, player);
  52.                     break;
  53.                 default:
  54.                     MoveHorizontal(dir, matrix, player);
  55.                     break;
  56.             }
  57.         }
  58.  
  59.         private static void MoveVertical(int dir, List<List<char>> matrix, Position player)
  60.         {
  61.             if (player.Row + dir >= matrix.Count)
  62.             {
  63.                 for (int currentRow = 0; currentRow < matrix.Count; currentRow++)
  64.                 {
  65.                     if (player.Col < matrix[currentRow].Count)
  66.                     {
  67.                         player.Row = currentRow;
  68.                         break;
  69.                     }
  70.                 }
  71.                
  72.             }
  73.             else if (player.Row + dir < 0)
  74.             {
  75.                 for (int currentRow = matrix.Count - 1; currentRow >= 0; currentRow--)
  76.                 {
  77.                     if (player.Col < matrix[currentRow].Count)
  78.                     {
  79.                         player.Row = currentRow;
  80.                         break;
  81.                     }
  82.                 }
  83.             }
  84.             else
  85.             {
  86.                 List<char> nextRow;
  87.  
  88.                 if (dir < 0)
  89.                 {
  90.                     nextRow = matrix.LastOrDefault(row => row.Count > player.Col && matrix.IndexOf(row) < player.Row);
  91.  
  92.                     if (nextRow == null)
  93.                     {
  94.                         nextRow = matrix.Last(row => row.Count > player.Col && matrix.IndexOf(row) >= player.Row);
  95.                     }
  96.                 }
  97.                 else
  98.                 {
  99.                     nextRow = matrix.FirstOrDefault(row => row.Count > player.Col && matrix.IndexOf(row) > player.Row);
  100.  
  101.                     if (nextRow == null)
  102.                     {
  103.                         nextRow = matrix.First(row => row.Count > player.Col && matrix.IndexOf(row) <= player.Row);
  104.                     }
  105.                 }
  106.  
  107.                
  108.                 player.Row = matrix.IndexOf(nextRow);
  109.             }
  110.         }
  111.  
  112.         private static void MoveHorizontal(int dir, List<List<char>> matrix, Position player)
  113.         {
  114.             if (dir > 0)
  115.             {
  116.                 player.Col = (player.Col + 1) % matrix[player.Row].Count;
  117.                 return;
  118.             }
  119.             player.Col = (player.Col - 1 + matrix[player.Row].Count) % matrix[player.Row].Count;
  120.         }
  121.        
  122.         private static int DirectionGetter(char direction)
  123.         {
  124.             if (direction == 'D') return 2;
  125.             if (direction == 'U') return -2;
  126.             if (direction == 'L') return -1;
  127.             return 1;
  128.         }
  129.  
  130.         private static List<List<char>> ReadMatrix(int size)
  131.         {
  132.             List<List<char>> matrix = new List<List<char>>();
  133.  
  134.             for (int i = 0; i < size; i++)
  135.             {
  136.                 matrix.Add(Console.ReadLine().ToCharArray().ToList());
  137.             }
  138.  
  139.             return matrix;
  140.         }
  141.     }
  142.  
  143.     public class Position
  144.     {
  145.         public int Row { get; set; }
  146.         public int Col { get; set; }
  147.  
  148.         public Position(int row, int col)
  149.         {
  150.             this.Row = row;
  151.             this.Col = col;
  152.         }
  153.     }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement