sivancheva

Portal

Oct 6th, 2017
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.49 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace _03_Portal
  8. {
  9. class Portal
  10. {
  11. static void Main(string[] args)
  12. {
  13. int n = int.Parse(Console.ReadLine());
  14.  
  15. char?[][] jagged = new char?[n][];
  16. int startRow = 0;
  17. int startCol = 0;
  18.  
  19. for (int i = 0; i < n; i++)
  20. {
  21. var input = Console.ReadLine().ToCharArray();
  22. jagged[i] = new char?[n];
  23. for (int j = 0; j < input.Length; j++)
  24. {
  25. jagged[i][j] = input[j];
  26. if (input[j] == 'S')
  27. {
  28. startRow = i;
  29. startCol = j;
  30. }
  31. }
  32. }
  33.  
  34. var commands = Console.ReadLine().ToCharArray();
  35. int currRow = startRow;
  36. int currCol = startCol;
  37. int stepsCounter = 0;
  38.  
  39. for (int i = 0; i < commands.Length; i++)
  40. {
  41. if (commands[i] == 'D')
  42. {
  43. if (currRow == n - 1)
  44. {
  45. currRow = 0;
  46. stepsCounter++;
  47. }
  48. else
  49. {
  50. currRow ++;
  51. stepsCounter++;
  52. }
  53.  
  54.  
  55. while (jagged[currRow][currCol] == null)
  56. {
  57. currRow++;
  58. stepsCounter++;
  59. }
  60.  
  61. if (jagged[currRow][currCol] == 'S' || jagged[currRow][currCol] == 'O')
  62. {
  63. currRow++;
  64. stepsCounter++;
  65. break;
  66. }
  67. else if (jagged[currRow][currCol] == 'E')
  68. {
  69. Console.WriteLine($"Experiment successful. {stepsCounter} turns required.");
  70. return;
  71. }
  72. }
  73. else if (commands[i] == 'U')
  74. {
  75. if (currRow == 0)
  76. {
  77. currRow = n-1;
  78. stepsCounter++;
  79. }
  80. else
  81. {
  82. currRow--;
  83. stepsCounter++;
  84. }
  85.  
  86. if (jagged[currRow][currCol] == null)
  87. {
  88. currRow--;
  89. stepsCounter++;
  90. }
  91. if (jagged[currRow][currCol] == 'S' || jagged[currRow][currCol] == 'O')
  92. {
  93. currRow--;
  94. stepsCounter++;
  95. break;
  96. }
  97. else if (jagged[currRow][currCol] == 'E')
  98. {
  99. Console.WriteLine($"Experiment successful. {stepsCounter} turns required.");
  100. return;
  101. }
  102. }
  103. else if (commands[i] == 'R')
  104. {
  105. if (currCol == n-1)
  106. {
  107. currCol = 0;
  108. stepsCounter++;
  109. }
  110. else
  111. {
  112. currCol++;
  113. stepsCounter++;
  114. }
  115.  
  116. if (!jagged[currRow][currCol].HasValue)
  117. {
  118. currCol++;
  119. stepsCounter++;
  120. }
  121. if (jagged[currRow][currCol] == 'S' || jagged[currRow][currCol] == 'O')
  122. {
  123. currCol++;
  124. stepsCounter++;
  125. break;
  126. }
  127. else if (jagged[currRow][currCol] == 'E')
  128. {
  129. Console.WriteLine($"Experiment successful. {stepsCounter} turns required.");
  130. return;
  131. }
  132. }
  133. else if (commands[i] == 'L')
  134. {
  135. if (currCol == 0)
  136. {
  137. currCol = n-1;
  138. stepsCounter++;
  139. }
  140. else
  141. {
  142. currCol--;
  143. stepsCounter++;
  144. }
  145.  
  146. if (!jagged[currRow][currCol].HasValue)
  147. {
  148. currCol--;
  149. stepsCounter++;
  150. }
  151.  
  152. if (jagged[currRow][currCol] == 'S' || jagged[currRow][currCol] == 'O')
  153. {
  154. currCol--;
  155. stepsCounter++;
  156. break;
  157. }
  158. else if (jagged[currRow][currCol] == 'E')
  159. {
  160. Console.WriteLine($"Experiment successful. {stepsCounter} turns required.");
  161. return;
  162. }
  163. }
  164.  
  165. }
  166. Console.WriteLine($"Robot stuck at {currRow} {currCol}. Experiment failed.");
  167. }
  168. }
  169. }
Advertisement
Add Comment
Please, Sign In to add comment