Advertisement
Guest User

Untitled

a guest
Nov 30th, 2016
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Portal
  6. {
  7. class Portal
  8. {
  9. static void Main()
  10. {
  11. int sizeOfMatrix = int.Parse(Console.ReadLine());
  12. var matrix = new char[sizeOfMatrix][];
  13.  
  14. int[] startPos = new int[2];
  15. for (int curRow = 0; curRow < sizeOfMatrix; curRow++)
  16. {
  17. List<char> lineCols = Console.ReadLine().ToCharArray().ToList();
  18. if (lineCols.Count < sizeOfMatrix)
  19. {
  20. int remaindSize = sizeOfMatrix - lineCols.Count;
  21. for (int empty = 0; empty < remaindSize; empty++)
  22. lineCols.Add('N');
  23. }
  24.  
  25. if (lineCols.Contains('S'))
  26. {
  27. string str = string.Join("", lineCols);
  28. int indexCol = str.IndexOf('S');
  29. startPos[0] = curRow;
  30. startPos[1] = indexCol;
  31. }
  32.  
  33. matrix[curRow] = string.Format("{0}", string.Join("", lineCols)).ToCharArray();
  34. }
  35.  
  36. int row = startPos[0];
  37. int col = startPos[1];
  38. char[] commands = Console.ReadLine().ToCharArray();
  39.  
  40. for (int pos = 0; pos < commands.Length; pos++)
  41. {
  42. if (commands[pos].Equals('D'))
  43. {
  44. row++;
  45.  
  46. if (row >= sizeOfMatrix)
  47. row = 0;
  48. }
  49. else if (commands[pos].Equals('U'))
  50. {
  51. row--;
  52.  
  53. if (row < 0)
  54. row = sizeOfMatrix - 1;
  55. }
  56. else if (commands[pos].Equals('L'))
  57. {
  58. col--;
  59.  
  60. if (col < 0)
  61. col = sizeOfMatrix - 1;
  62. }
  63. else if (commands[pos].Equals('R'))
  64. {
  65. col++;
  66.  
  67. if (col >= sizeOfMatrix)
  68. col = 0;
  69. }
  70.  
  71. if (matrix[row][col].Equals('N'))
  72. {
  73. pos--;
  74. continue;
  75. }
  76. else if (matrix[row][col].Equals('E'))
  77. {
  78. Console.WriteLine("Experiment successful. {0} turns required.", pos + 1);
  79. break;
  80. }
  81.  
  82. if (pos.Equals(commands.Length - 1))
  83. {
  84. Console.WriteLine("Robot stuck at {0} {1}. Experiment failed.", row, col);
  85. }
  86. }
  87. }
  88. }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement