Advertisement
butoff

Parking System 80

Jan 30th, 2018
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.83 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.             string command = "";
  16.             while ((command = Console.ReadLine()) != "stop")
  17.             {
  18.                 int[] data = command.Split(' ').Select(int.Parse).ToArray();
  19.                 int entrance = data[0];
  20.                 int row = data[1];
  21.                 int col = data[2];
  22.                 int steps = Math.Abs(entrance - row) + 1;   // initial steps in first (0) column
  23.  
  24.                 if (matrix[row, col] == 0)
  25.                 {
  26.                     matrix[row, col] = 1;
  27.                     steps += col;                           // add steps in the row to the initial steps
  28.                     Console.WriteLine(steps);
  29.                 }                                              
  30.                 else                                        // lower -- v v v v   v v v v v --upper
  31.                 {                                           // row-->|0|_|_|_|_|X|_|_|_|_|_|  
  32.                     int cnt = 1;
  33.                     while (true)
  34.                     {
  35.                         int lowerCell = col - cnt;              // the cells before desired cell
  36.                         int upperCell = col + cnt;              // the cells after desired cell
  37.  
  38.                         if (lowerCell < 1 && upperCell > columns - 1)  // if cells are out of bounds
  39.                         {
  40.                             Console.WriteLine($"Row {row} full");
  41.                             break;
  42.                         }
  43.                         if (lowerCell > 0 && matrix[row,lowerCell] == 0)       // if cell is inside of the row
  44.                         {                                                       // and free
  45.                             matrix[row,lowerCell] = 1;
  46.                             steps += lowerCell;
  47.                             Console.WriteLine(steps);
  48.                             break;
  49.                         }
  50.                         if (upperCell < columns && matrix[row,upperCell] == 0)  // if cell is inside of the row
  51.                         {                                                        // and is free
  52.                             matrix[row,upperCell] = 1;
  53.                             steps += upperCell;
  54.                             Console.WriteLine(steps);
  55.                             break;
  56.                         }
  57.                         cnt++;
  58.                     }
  59.                 }
  60.             }
  61.         }        
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement