Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package MultidimensionalArrays2;
- import java.util.Scanner;
- public class ParkingSystem {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String[] matrixSize = scanner.nextLine().split("\\s+");
- boolean[][] matrix = new boolean[Integer.parseInt(matrixSize[0])][Integer.parseInt(matrixSize[1])];
- for (int index = 0; index < matrix.length; index++) {
- matrix[index][0] = true;
- }
- while (true) {
- String command = scanner.nextLine();
- if (command.equals("stop")) {
- break;
- }
- String[] input = command.split("\\s+");
- int data = Integer.parseInt(input[0]);
- int row = Integer.parseInt(input[1]);
- int col = Integer.parseInt(input[2]);
- int distance = Math.abs(data - row) + 1;
- int leftCol = 0 >= col - 1 ? 1 : col - 1;
- int rightCol = col + 1 >= matrix[0].length - 1 ? col : col + 1;
- while (matrix[row][leftCol]) {
- if (leftCol == 0) {
- break;
- }
- leftCol--;
- }
- while (matrix[row][rightCol]) {
- if (rightCol == matrix[0].length - 1) {
- break;
- }
- rightCol++;
- }
- if (isRowFull(row, matrix)) {
- System.out.printf("Row %d full%n", row);
- continue;
- }
- if (!matrix[row][col]) {
- matrix[row][col] = true;
- distance += col;
- System.out.println(distance);
- continue;
- } else {
- if ((col - leftCol) > (Math.abs(rightCol - col))) {
- col = rightCol;
- } else {
- col = leftCol;
- }
- }
- if (col <= 1 && matrix[row][col]) {
- col = rightCol;
- }
- if (matrix[row][col] && rightCol == matrix[0].length - 1) {
- col = leftCol;
- }
- matrix[row][col] = true;
- distance += col;
- System.out.println(distance);
- }
- }
- public static boolean isRowFull(int row, boolean[][] matrix) {
- for (int i = 1; i < matrix[0].length; i++) {
- if (!matrix[row][i]) {
- return false;
- }
- }
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement