daily pastebin goal
41%
SHARE
TWEET

Untitled

butoff Jan 29th, 2018 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Linq;
  3.  
  4.  
  5. namespace t11
  6. {
  7.     class Program
  8.     {
  9.         static void Main()
  10.         {
  11.             string[] input = Console.ReadLine().Split();
  12.             int rows = int.Parse(input[0]);
  13.             int columns = int.Parse(input[1]);
  14.             byte[,] matrix = new byte[rows, columns];
  15.  
  16.             string command = Console.ReadLine();
  17.             while ((command = Console.ReadLine()) != "stop")
  18.             {
  19.                 int[] data = command.Split(' ').Select(int.Parse).ToArray();
  20.                 int entrance = data[0];
  21.                 int row = data[1];
  22.                 int col = data[2];
  23.                 int steps = Math.Abs(entrance - row) + 1;   // initial steps in first column                                
  24.  
  25.                 if (IsParked(matrix, row, col))
  26.                 {                    
  27.                     steps += col;                           // add steps in the row to initial
  28.                     Console.WriteLine(steps);
  29.                 }
  30.                 else
  31.                 {
  32.                     int lowerCounter = col - 1;             // the cells before the desired cell
  33.                     int upperCounter = columns - col - 1;   // the cells after the desired cell
  34.  
  35.                     while (true)
  36.                     {
  37.                         if (lowerCounter == 0 && upperCounter == 0) // if no more free cells
  38.                         {
  39.                             Console.WriteLine($"Row {row} full");
  40.                             break;
  41.                         }
  42.                         if (lowerCounter > 0)                        
  43.                         {
  44.                             col = lowerCounter;
  45.                             if (IsParked(matrix, row, col))
  46.                             {                              
  47.                                 steps += col;
  48.                                 Console.WriteLine(steps);
  49.                                 break;
  50.                             }
  51.                             lowerCounter--;
  52.                         }
  53.                         if (upperCounter > 0)
  54.                         {
  55.                             col = columns - upperCounter;
  56.                             if (IsParked(matrix, row, col))
  57.                             {                                
  58.                                 steps += col;
  59.                                 Console.WriteLine(steps);
  60.                                 break;
  61.                             }
  62.                             upperCounter--;
  63.                         }
  64.                     }
  65.                 }    
  66.             }
  67.         }
  68.         static bool IsParked(byte[,] matrix, int row, int col)      // check the desired cell
  69.         {
  70.             if (matrix[row, col] == 0)
  71.             {
  72.                 matrix[row, col] = 1;
  73.                 return true;
  74.             }            
  75.             return false;            
  76.         }
  77.     }
  78. }
RAW Paste Data
Pastebin PRO WINTER Special!
Get 40% OFF Pastebin PRO accounts!
Top