butoff

Problem 11. Parking System

Jan 30th, 2018
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.22 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.  
  25.                 if (IsParked(matrix, row, col))            
  26.                 {
  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 lowerCounter = col - 1;             // the cells below desired cell without(0)
  33.                     int upperCounter = columns - col - 1;   // the cells after desired cell
  34.  
  35.                     while (true)
  36.                     {                        
  37.                         if (lowerCounter > 0)                       // on every loop check lower cells
  38.                         {
  39.                             col = lowerCounter;
  40.                             if (IsParked(matrix, row, col))
  41.                             {
  42.                                 steps += col;
  43.                                 Console.WriteLine(steps);
  44.                                 break;
  45.                             }
  46.                             lowerCounter--;
  47.                         }
  48.                         if (upperCounter > 0)                       // on every loop check upper cells
  49.                         {
  50.                             col = columns - upperCounter;         // count of matrix columns - upperCounter
  51.                             if (IsParked(matrix, row, col))
  52.                             {
  53.                                 steps += col;
  54.                                 Console.WriteLine(steps);
  55.                                 break;
  56.                             }
  57.                             upperCounter--;                      
  58.                         }
  59.                         if (lowerCounter == 0 && upperCounter == 0)     // look for unchecked cells
  60.                         {
  61.                             Console.WriteLine($"Row {row} full");
  62.                             break;
  63.                         }
  64.                     }
  65.                 }
  66.             }
  67.         }
  68.         static bool IsParked(byte[,] matrix, int row, int col)
  69.         {
  70.             if (matrix[row, col] == 0)
  71.             {
  72.                 matrix[row, col] = 1;
  73.                 return true;
  74.             }
  75.             return false;
  76.         }
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment