Advertisement
Guest User

Multidimensional Arrays, softUniParkingProblem

a guest
Jan 24th, 2020
401
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.84 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.ArrayList;
  5. import java.util.Arrays;
  6. import java.util.List;
  7.  
  8. public class firstStQexercise {
  9.     public static void main(String[] args) throws IOException {
  10.         BufferedReader reader =
  11.                 new BufferedReader(new InputStreamReader(System.in));
  12.  
  13.         int[] dimension = Arrays.stream(reader.readLine().
  14.                 split("\\s+")).mapToInt(Integer::parseInt).toArray();
  15.  
  16.         int rows = dimension[0];
  17.         int cols = dimension[1];
  18.  
  19.        List<ArrayList<Boolean>> parkingLot = new ArrayList<>();
  20.  
  21.         booleanMatrixInitialisation(parkingLot, rows, cols);
  22.         String input = reader.readLine();
  23.  
  24.         while (!input.equals("stop")) {
  25.             String[] coordinates = input.split("\\s+");
  26.             int entrance = Integer.parseInt(coordinates[0]);
  27.             int row = Integer.parseInt(coordinates[1]);
  28.             int col = Integer.parseInt(coordinates[2]);
  29.  
  30.             if (parkingLot.get(row).get(col)) {
  31.                 parkingLot.get(row).set(col, false);
  32.                 System.out.println(Math.abs(row - entrance + col + 1));
  33.             } else {
  34.                 int leftIndex = col - 1;
  35.                 int rightIndex = col + 1;
  36.                 boolean isLeftDone = false;
  37.                 boolean isRightDone = false;
  38.  
  39.                 while ((leftIndex > 0 || rightIndex < cols)) {
  40.                     if (leftIndex > 0 && parkingLot.get(row).get(leftIndex)) {
  41.                         parkingLot.get(row).set(leftIndex, false);
  42.                         isLeftDone = true;
  43.                         System.out.println(Math.abs(row - entrance + leftIndex + 1));
  44.                         break;
  45.                     } else if (rightIndex < cols && parkingLot.get(row).get(rightIndex)) {
  46.                         parkingLot.get(row).set(rightIndex, false);
  47.                         isRightDone = true;
  48.                         System.out.println(Math.abs(row - entrance + rightIndex + 1));
  49.                         break;
  50.                     }
  51.                     leftIndex--;
  52.                     rightIndex++;
  53.                 }
  54.  
  55.                 if (!isLeftDone && !isRightDone)
  56.                     System.out.printf("Row %d full%n", row);
  57.             }
  58.             input = reader.readLine();
  59.         }
  60.     }
  61.     private static void booleanMatrixInitialisation (List<ArrayList<Boolean>> matrix, int rows, int cols){
  62.         for (int r = 0; r < rows; r++) {
  63.             ArrayList<Boolean> current = new ArrayList<>();
  64.             for (int c = 0; c < cols; c++) {
  65.                 if (c != 0) {
  66.                     current.add(true);
  67.                 }
  68.                 else {
  69.                     current.add(false);
  70.                 }
  71.             }
  72.             matrix.add(current);
  73.         }
  74.     }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement