Advertisement
Guest User

Untitled

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