Advertisement
Didart

Parking System

Jan 14th, 2023
624
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.51 KB | None | 0 0
  1. package MultidimensionalArrays2;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class ParkingSystem {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.  
  9.         String[] matrixSize = scanner.nextLine().split("\\s+");
  10.  
  11.         boolean[][] matrix = new boolean[Integer.parseInt(matrixSize[0])][Integer.parseInt(matrixSize[1])];
  12.  
  13.         for (int index = 0; index < matrix.length; index++) {
  14.             matrix[index][0] = true;
  15.         }
  16.  
  17.         while (true) {
  18.             String command = scanner.nextLine();
  19.             if (command.equals("stop")) {
  20.                 break;
  21.             }
  22.             String[] input = command.split("\\s+");
  23.  
  24.             int data = Integer.parseInt(input[0]);
  25.             int row = Integer.parseInt(input[1]);
  26.             int col = Integer.parseInt(input[2]);
  27.  
  28.             int distance = Math.abs(data - row) + 1;
  29.             int leftCol = 0 >= col - 1 ? 1 : col - 1;
  30.             int rightCol = col + 1 >= matrix[0].length - 1 ? col : col + 1;
  31.  
  32.             while (matrix[row][leftCol]) {
  33.                 if (leftCol == 0) {
  34.                     break;
  35.                 }
  36.                 leftCol--;
  37.             }
  38.             while (matrix[row][rightCol]) {
  39.                 if (rightCol == matrix[0].length - 1) {
  40.                     break;
  41.                 }
  42.                 rightCol++;
  43.             }
  44.             if (isRowFull(row, matrix)) {
  45.                 System.out.printf("Row %d full%n", row);
  46.                 continue;
  47.             }
  48.             if (!matrix[row][col]) {
  49.                 matrix[row][col] = true;
  50.                 distance += col;
  51.                 System.out.println(distance);
  52.                 continue;
  53.             } else {
  54.                 if ((col - leftCol) > (Math.abs(rightCol - col))) {
  55.                     col = rightCol;
  56.                 } else {
  57.                     col = leftCol;
  58.                 }
  59.             }
  60.             if (col <= 1 && matrix[row][col]) {
  61.                 col = rightCol;
  62.             }
  63.             if (matrix[row][col] && rightCol == matrix[0].length - 1) {
  64.                 col = leftCol;
  65.             }
  66.             matrix[row][col] = true;
  67.             distance += col;
  68.             System.out.println(distance);
  69.  
  70.         }
  71.     }
  72.  
  73.     public static boolean isRowFull(int row, boolean[][] matrix) {
  74.         for (int i = 1; i < matrix[0].length; i++) {
  75.             if (!matrix[row][i]) {
  76.                 return false;
  77.             }
  78.         }
  79.         return true;
  80.     }
  81. }
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement