Advertisement
butoff

Parking System

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