Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Linq.Expressions;
- using System.Text;
- using System.Threading.Tasks;
- namespace _11.Parking_System
- {
- public class ParkingSystem
- {
- public static void Main()
- {
- int[] sizes = Console.ReadLine()
- .Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
- .Select(int.Parse)
- .ToArray();
- int[,] parkingLot = new int[sizes[0], sizes[1]];
- string line = Console.ReadLine();
- while (line != "stop")
- {
- int[] coordinates = line
- .Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
- .Select(int.Parse)
- .ToArray();
- int entryRow = coordinates[0];
- int desiredRow = coordinates[1];
- int desiredCol = coordinates[2];
- if (IsInRange(parkingLot, entryRow, desiredRow, desiredCol))
- {
- if (parkingLot[desiredRow, desiredCol] == 0)
- {
- FindDistance(parkingLot, entryRow, desiredRow, desiredCol);
- }
- else if (parkingLot[desiredRow, desiredCol] == 1 && HasFreeSpot(parkingLot, entryRow, desiredRow, desiredCol))
- {
- FindClosestSpot(parkingLot, entryRow, desiredRow, desiredCol);
- }
- else
- {
- Console.WriteLine($"Row {desiredRow} full");
- }
- }
- line = Console.ReadLine();
- }
- }
- private static bool IsInRange(int[,] parkingLot, int entryRow, int desiredRow, int desiredCol)
- {
- if (entryRow < parkingLot.GetLength(0)
- && entryRow >= 0
- && desiredRow < parkingLot.GetLength(0)
- && desiredRow >= 0
- && desiredCol >= 1
- && desiredCol < parkingLot.GetLength(1))
- {
- return true;
- }
- return false;
- }
- private static bool HasFreeSpot(int[,] parkingLot, int entryRow, int desiredRow, int desiredCol)
- {
- for (int col = 1; col < parkingLot.GetLength(1); col++)
- {
- if (parkingLot[desiredRow, col] == 0)
- {
- return true;
- }
- }
- return false;
- }
- private static void FindClosestSpot(int[,] parkingLot, int entryRow, int desiredRow, int desiredCol)
- {
- int leftIndex = int.MaxValue;
- int rightIndex = int.MaxValue;
- for (int col = desiredCol; col >= 1; col--)
- {
- if (parkingLot[desiredRow, col] == 0)
- {
- leftIndex = col;
- break;
- }
- }
- for (int col = desiredCol; col < parkingLot.GetLength(1); col++)
- {
- if (parkingLot[desiredRow, col] == 0)
- {
- rightIndex = col;
- break;
- }
- }
- if (Math.Abs(desiredCol - leftIndex) <= Math.Abs(rightIndex - desiredCol))
- {
- FindDistance(parkingLot, entryRow, desiredRow, leftIndex);
- }
- else
- {
- FindDistance(parkingLot, entryRow, desiredRow, rightIndex);
- }
- }
- private static void FindDistance(int[,] parkingLot, int entryRow, int desiredRow, int desiredCol)
- {
- if (entryRow <= desiredRow)
- {
- int distance = desiredRow - entryRow + 1 + desiredCol;
- parkingLot[desiredRow, desiredCol] = 1;
- Console.WriteLine(distance);
- }
- else
- {
- int distance = entryRow - desiredRow + 1 + desiredCol;
- parkingLot[desiredRow, desiredCol] = 1;
- Console.WriteLine(distance);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment