damyan91

Untitled

Jun 6th, 2017
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.22 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Linq.Expressions;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace _11.Parking_System
  9. {
  10. public class ParkingSystem
  11. {
  12. public static void Main()
  13. {
  14. int[] sizes = Console.ReadLine()
  15. .Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
  16. .Select(int.Parse)
  17. .ToArray();
  18.  
  19. int[,] parkingLot = new int[sizes[0], sizes[1]];
  20.  
  21. string line = Console.ReadLine();
  22.  
  23. while (line != "stop")
  24. {
  25. int[] coordinates = line
  26. .Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
  27. .Select(int.Parse)
  28. .ToArray();
  29.  
  30. int entryRow = coordinates[0];
  31. int desiredRow = coordinates[1];
  32. int desiredCol = coordinates[2];
  33.  
  34. if (IsInRange(parkingLot, entryRow, desiredRow, desiredCol))
  35. {
  36. if (parkingLot[desiredRow, desiredCol] == 0)
  37. {
  38. FindDistance(parkingLot, entryRow, desiredRow, desiredCol);
  39. }
  40.  
  41. else if (parkingLot[desiredRow, desiredCol] == 1 && HasFreeSpot(parkingLot, entryRow, desiredRow, desiredCol))
  42. {
  43. FindClosestSpot(parkingLot, entryRow, desiredRow, desiredCol);
  44. }
  45. else
  46. {
  47. Console.WriteLine($"Row {desiredRow} full");
  48. }
  49. }
  50.  
  51. line = Console.ReadLine();
  52. }
  53. }
  54.  
  55. private static bool IsInRange(int[,] parkingLot, int entryRow, int desiredRow, int desiredCol)
  56. {
  57. if (entryRow < parkingLot.GetLength(0)
  58. && entryRow >= 0
  59. && desiredRow < parkingLot.GetLength(0)
  60. && desiredRow >= 0
  61. && desiredCol >= 1
  62. && desiredCol < parkingLot.GetLength(1))
  63. {
  64. return true;
  65. }
  66. return false;
  67. }
  68.  
  69. private static bool HasFreeSpot(int[,] parkingLot, int entryRow, int desiredRow, int desiredCol)
  70. {
  71. for (int col = 1; col < parkingLot.GetLength(1); col++)
  72. {
  73. if (parkingLot[desiredRow, col] == 0)
  74. {
  75. return true;
  76. }
  77. }
  78.  
  79. return false;
  80. }
  81.  
  82. private static void FindClosestSpot(int[,] parkingLot, int entryRow, int desiredRow, int desiredCol)
  83. {
  84. int leftIndex = int.MaxValue;
  85. int rightIndex = int.MaxValue;
  86.  
  87. for (int col = desiredCol; col >= 1; col--)
  88. {
  89. if (parkingLot[desiredRow, col] == 0)
  90. {
  91. leftIndex = col;
  92. break;
  93. }
  94. }
  95.  
  96. for (int col = desiredCol; col < parkingLot.GetLength(1); col++)
  97. {
  98. if (parkingLot[desiredRow, col] == 0)
  99. {
  100. rightIndex = col;
  101. break;
  102. }
  103. }
  104.  
  105. if (Math.Abs(desiredCol - leftIndex) <= Math.Abs(rightIndex - desiredCol))
  106. {
  107. FindDistance(parkingLot, entryRow, desiredRow, leftIndex);
  108. }
  109. else
  110. {
  111. FindDistance(parkingLot, entryRow, desiredRow, rightIndex);
  112. }
  113.  
  114. }
  115.  
  116. private static void FindDistance(int[,] parkingLot, int entryRow, int desiredRow, int desiredCol)
  117. {
  118.  
  119. if (entryRow <= desiredRow)
  120. {
  121. int distance = desiredRow - entryRow + 1 + desiredCol;
  122. parkingLot[desiredRow, desiredCol] = 1;
  123. Console.WriteLine(distance);
  124. }
  125. else
  126. {
  127. int distance = entryRow - desiredRow + 1 + desiredCol;
  128. parkingLot[desiredRow, desiredCol] = 1;
  129. Console.WriteLine(distance);
  130. }
  131. }
  132. }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment