Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace _02.ParkingSystem
- {
- using System;
- using System.Linq;
- public class ParkingSystemMain
- {
- public static void Main(string[] args)
- {
- int[] gridParameters =
- Console.ReadLine()
- .Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
- .Select(int.Parse)
- .ToArray();
- int gridRows = gridParameters[0];
- int gridCols = gridParameters[1];
- char[,] grid = new char[gridRows, gridCols];
- string command = Console.ReadLine();
- while (command != "stop")
- {
- int[] commandInfo = command.Split(' ').Select(int.Parse).ToArray();
- int startingRow = commandInfo[0];
- int spotRow = commandInfo[1];
- int spotCol = commandInfo[2];
- string message = string.Empty;
- if (grid[spotRow, spotCol] != 'c')
- {
- grid[spotRow, spotCol] = 'c';
- message = CalculateDistance(startingRow, spotRow, spotCol).ToString();
- }
- else
- {
- int indexCheck = 1;
- while (grid[spotRow, 0] != 'c')
- {
- if (spotCol - indexCheck >= 0)
- {
- int currentCol = spotCol - indexCheck;
- if (currentCol == 0)
- {
- message = string.Format($"Row {spotRow} full");
- break;
- }
- if (grid[spotRow, currentCol] != 'c')
- {
- grid[spotRow, currentCol] = 'c';
- message = CalculateDistance(startingRow, spotRow, currentCol).ToString();
- break;
- }
- }
- if (spotCol + indexCheck < gridCols)
- {
- int currentCol = spotCol + indexCheck;
- if (grid[spotRow, currentCol] != 'c')
- {
- grid[spotRow, currentCol] = 'c';
- message = CalculateDistance(startingRow, spotRow, currentCol).ToString();
- break;
- }
- }
- indexCheck++;
- }
- }
- Console.WriteLine(message);
- command = Console.ReadLine();
- }
- }
- private static int CalculateDistance(int startingRow, int spotRow, int spotCol)
- {
- int result = spotCol + 1;
- result += Math.Abs(startingRow - spotRow);
- return result;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement