×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Added: Dec 13, 2020 8:42 PM
Views: 4644
Tags: twodimarray
  1.  
  2. // TODO: 08.12.2020  Ein zweidimensionales Array speichert, welche Plätze eines Kino
  3. //  vergeben sind (0 = frei, 1 = belegt).
  4. //  .
  5. //  Schreiben Sie eine Methode, die das gesamte Kino nach n
  6. //  nebeneinanderliegenden freien Plätzen durchsucht. Die Methode
  7. //  gibt true oder false zurück, je nachdem, ob es Plätze findet. Die
  8. //  Methode soll außerdem die erste gefundene Position (Zeile und
  9. //  Spalte in Java-Zählweise, also 1. Reihe bzw. 1. Platz = 0) am
  10. //  Bildschirm ausgeben.
  11. public class Main {
  12.  
  13.     public static void main(String[] args) {
  14.         int [][] kino = {
  15.                 {1 , 1 , 0 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1} ,
  16.                 {1 , 1 , 1 , 0 , 0 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1} ,
  17.                 {1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1} ,
  18.                 {1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1} ,
  19.                 {1 , 1 , 1 , 1 , 0 , 0 , 1 , 1 , 1 , 1 , 1 , 1 , 1} ,
  20.                 {0 , 0 , 0 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 0} ,
  21.                 {0 , 0 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 0} ,
  22.                 {0 , 0 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 0 , 0 , 0 , 0} ,
  23.                 {0 , 0 , 0 , 0 , 1 , 1 , 1 , 1 , 0 , 0 , 0 , 0 , 0}
  24.         };
  25.         //System.out.println(seatTest(2,kino));
  26.          boolean check =seatTest(3,kino);
  27.         System.out.println(check);
  28.  
  29.  
  30.     }
  31.     public static boolean seatTest (int howManySeats, int[][] data){
  32.  
  33.         boolean seats = false;
  34.         boolean problem = false;
  35.         // howManySeats -1, because input is easier for humans to read
  36.         howManySeats--;
  37.  
  38.         for(int row=0; row< data.length; row++){
  39.             for(int col=0; col<data[row].length; col++){
  40.                 // if free seat is detected -> check if the seat int howManySeats next to it is also free
  41.                 if(data[row][col]==0){
  42.                     // check if there are enough seats or row is too short
  43.                     if(col+howManySeats>=data[row].length){
  44.                         //System.out.println("not enough seats... skipping");
  45.                     } else {
  46.                         // seat howManySeats next to it is also free so check if seats in between are free
  47.                         if (data[row][col + howManySeats] == 0) {
  48.                             for (int i = col + howManySeats; i > col; i--) {
  49.                                 // one seat is not free boolean problem is true
  50.                                 if (data[row][i] == 1) {
  51.                                     problem = true;
  52.                                 }
  53.                             }
  54.                             // if there is no problem a free space was found
  55.                             // seats = true could be replaced with return so only first gap will be put out
  56.                             if (problem == false) {
  57.                                 System.out.println(howManySeats + 1 + " free seats found at Row " + row + " and Column " + col + "!");
  58.                                 seats = true;
  59.                             }
  60.                         }
  61.                     }
  62.                     // raise col to not check gaps twice
  63.                     col = col + howManySeats+1;
  64.                 }
  65.             }
  66.         }
  67.  
  68.  
  69.  
  70.         return seats;
  71.     }
  72.    
  73.