Advertisement
Ligh7_of_H3av3n

09. Parking System

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