Advertisement
Guest User

P11.ParkingSystem

a guest
Jan 31st, 2018
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace t11
  5. {
  6. class Program
  7. {
  8. static void Main()
  9. {
  10. string[] input = Console.ReadLine().Split();
  11. int rows = int.Parse(input[0]);
  12. int columns = int.Parse(input[1]);
  13. // byte[,] matrix = new byte[rows, columns]; // matrix filled with 0
  14. // 0 for empty, 1 for occupied
  15.  
  16. byte[][] matrix = new byte[rows][];
  17.  
  18. string command = "";
  19. while ((command = Console.ReadLine()) != "stop")
  20. {
  21. int[] data = command.Split(' ').Select(int.Parse).ToArray();
  22. int entrance = data[0];
  23. int row = data[1];
  24. int col = data[2];
  25. int steps = Math.Abs(entrance - row) + 1; // initial steps in first (0) column
  26.  
  27. if (matrix[row] == null)
  28. {
  29. matrix[row] = new byte[columns]; // Initialize current row, if do not exist!
  30. }
  31.  
  32. if (matrix[row][col] == 0)
  33. {
  34. matrix[row][col] = 1;
  35. steps += col; // add steps in the row to the initial steps
  36. Console.WriteLine(steps);
  37. }
  38. else // lower -- v v v v v v v v v --upper
  39. { // row-->|0|_|_|_|_|X|_|_|_|_|_|
  40. int cnt = 1;
  41. while (true)
  42. {
  43. int lowerCell = col - cnt; // the cells before desired cell
  44. int upperCell = col + cnt; // the cells after desired cell
  45.  
  46. if (lowerCell < 1 && upperCell > columns - 1) // if cells are out of bounds
  47. {
  48. Console.WriteLine($"Row {row} full");
  49. break;
  50. }
  51. if (lowerCell > 0 && matrix[row][lowerCell] == 0) // if cell is inside of the row
  52. { // and free
  53. matrix[row][lowerCell] = 1;
  54. steps += lowerCell;
  55. Console.WriteLine(steps);
  56. break;
  57. }
  58. if (upperCell < columns && matrix[row][upperCell] == 0) // if cell is inside of the row
  59. { // and is free
  60. matrix[row][upperCell] = 1;
  61. steps += upperCell;
  62. Console.WriteLine(steps);
  63. break;
  64. }
  65. cnt++;
  66. }
  67. }
  68. }
  69. }
  70. }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement