Advertisement
bullit3189

Parking Feud

Jun 10th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace _02ParkingFeud
  5. {
  6. class Program
  7. {
  8.  
  9. static int rows = 0;
  10. static int cols = 0;
  11. static void Main(string[] args)
  12. {
  13. var matrixSize = Console.ReadLine().Split().Select(int.Parse).ToArray();
  14. rows = matrixSize[0];
  15. cols = matrixSize[1];
  16. int totalSteps = 0;
  17.  
  18. int entrance = int.Parse(Console.ReadLine());
  19.  
  20. while (true)
  21. {
  22. var input = Console.ReadLine().Split();
  23. string samSpot = input[entrance - 1];
  24. int samSteps = GetSteps(entrance, samSpot);
  25. bool isParked = true;
  26.  
  27. for (int i = 0; i < input.Length; i++)
  28. {
  29. bool isSameSpot = input[i] == samSpot;
  30. bool isDifferentRow = i + 1 != entrance;
  31.  
  32. if (isSameSpot && isDifferentRow)
  33. {
  34. int otherDriverSteps = GetSteps(i + 1, input[i]);
  35.  
  36. if (otherDriverSteps < samSteps)
  37. {
  38. totalSteps += samSteps * 2;
  39. isParked = false;
  40. break;
  41. }
  42. }
  43. }
  44.  
  45. if (isParked)
  46. {
  47. totalSteps += samSteps;
  48. Console.WriteLine($"Parked successfully at {input[entrance - 1]}.");
  49. Console.WriteLine($"Total Distance Passed: {totalSteps}");
  50. return;
  51. }
  52. }
  53. }
  54.  
  55. public static int GetSteps(int samRow, string samSpot)
  56. {
  57. int parkingRow = int.Parse(samSpot.Substring(1));
  58. int parkingCol = samSpot[0] - 'A' + 1;
  59.  
  60. int currentSteps = 0;
  61. bool moveRight = true;
  62.  
  63. while (parkingRow != samRow && parkingRow - 1 != samRow)
  64. {
  65. currentSteps += cols + 3;
  66.  
  67. if (samRow > parkingRow)
  68. {
  69. samRow--;
  70. }
  71.  
  72. else if (samRow < parkingRow)
  73. {
  74. samRow++;
  75. }
  76.  
  77. moveRight = !moveRight;
  78. }
  79.  
  80. if (moveRight)
  81. {
  82. currentSteps += parkingCol;
  83. }
  84.  
  85. else
  86. {
  87. currentSteps += cols - parkingCol + 1;
  88. }
  89.  
  90. return currentSteps;
  91. }
  92. }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement