Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.List;
- public class firstStQexercise {
- public static void main(String[] args) throws IOException {
- BufferedReader reader =
- new BufferedReader(new InputStreamReader(System.in));
- int[] dimension = Arrays.stream(reader.readLine().
- split("\\s+")).mapToInt(Integer::parseInt).toArray();
- int rows = dimension[0];
- int cols = dimension[1];
- List<ArrayList<Boolean>> parkingLot = new ArrayList<>();
- booleanMatrixInitialisation(parkingLot, rows, cols);
- String input = reader.readLine();
- while (!input.equals("stop")) {
- String[] coordinates = input.split("\\s+");
- int entrance = Integer.parseInt(coordinates[0]);
- int row = Integer.parseInt(coordinates[1]);
- int col = Integer.parseInt(coordinates[2]);
- if (parkingLot.get(row).get(col)) {
- parkingLot.get(row).set(col, false);
- System.out.println(Math.abs(row - entrance + col + 1));
- } else {
- int leftIndex = col - 1;
- int rightIndex = col + 1;
- boolean isLeftDone = false;
- boolean isRightDone = false;
- while ((leftIndex > 0 || rightIndex < cols)) {
- if (leftIndex > 0 && parkingLot.get(row).get(leftIndex)) {
- parkingLot.get(row).set(leftIndex, false);
- isLeftDone = true;
- System.out.println(Math.abs(row - entrance + leftIndex + 1));
- break;
- } else if (rightIndex < cols && parkingLot.get(row).get(rightIndex)) {
- parkingLot.get(row).set(rightIndex, false);
- isRightDone = true;
- System.out.println(Math.abs(row - entrance + rightIndex + 1));
- break;
- }
- leftIndex--;
- rightIndex++;
- }
- if (!isLeftDone && !isRightDone)
- System.out.printf("Row %d full%n", row);
- }
- input = reader.readLine();
- }
- }
- private static void booleanMatrixInitialisation (List<ArrayList<Boolean>> matrix, int rows, int cols){
- for (int r = 0; r < rows; r++) {
- ArrayList<Boolean> current = new ArrayList<>();
- for (int c = 0; c < cols; c++) {
- if (c != 0) {
- current.add(true);
- }
- else {
- current.add(false);
- }
- }
- matrix.add(current);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement