MeGaDeTH_91

11

Feb 1st, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _11._Parking_System
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. int[] matrixDimensions = Console.ReadLine()
  12. .Split(new[] { ' ' },StringSplitOptions.RemoveEmptyEntries)
  13. .Select(int.Parse)
  14. .ToArray();
  15.  
  16. int rows = matrixDimensions[0];
  17. int cols = matrixDimensions[1];
  18.  
  19. Dictionary<int, HashSet<int>> parking = new Dictionary<int, HashSet<int>>();
  20. for (int i = 0; i < rows; i++)
  21. {
  22. parking[i] = new HashSet<int>();
  23. }
  24.  
  25.  
  26. string command;
  27. while ((command = Console.ReadLine()) != "stop")
  28. {
  29. int[] parkingDetails = command
  30. .Split(new[] {' '},StringSplitOptions.RemoveEmptyEntries)
  31. .Select(int.Parse)
  32. .ToArray();
  33.  
  34. int entryRow = parkingDetails[0];
  35. int desiredRow = parkingDetails[1];
  36. int desiredCol = parkingDetails[2];
  37.  
  38. int parkIndex = 0;
  39.  
  40. if (IsFree(parking, desiredRow, desiredCol))
  41. parkIndex = desiredCol;
  42.  
  43. else
  44. {
  45. for (int i = 1; i < cols - 1; i++)
  46. {
  47. int previous = desiredCol - i;
  48. int next = desiredCol + i;
  49.  
  50. if (previous > 0 && // First col is unreachable by condition!
  51. IsFree(parking, desiredRow, (previous))
  52. )
  53. {
  54. parkIndex = previous;
  55. break;
  56. }
  57.  
  58. else if (next < cols &&
  59. IsFree(parking, desiredRow, (next))
  60. )
  61. {
  62. parkIndex = next;
  63. break;
  64. }
  65. }
  66. }
  67.  
  68. if (parkIndex == 0) // Not valid!
  69. Console.WriteLine($"Row {desiredRow} full");
  70.  
  71. else
  72. {
  73. parking[desiredRow].Add(parkIndex);
  74. int steps = Math.Abs(entryRow - desiredRow) + 1 + parkIndex;
  75. Console.WriteLine(steps);
  76. }
  77. }
  78. }
  79.  
  80. private static bool IsFree(Dictionary<int, HashSet<int>> parking, int row, int col)
  81. {
  82. if (!parking.ContainsKey(row) ||
  83. !parking[row].Contains(col)
  84. )
  85. {
  86. return true;
  87. }
  88. return false;
  89. }
  90. }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment