// TODO: 08.12.2020 Ein zweidimensionales Array speichert, welche Plätze eines Kino
// vergeben sind (0 = frei, 1 = belegt).
// .
// Schreiben Sie eine Methode, die das gesamte Kino nach n
// nebeneinanderliegenden freien Plätzen durchsucht. Die Methode
// gibt true oder false zurück, je nachdem, ob es Plätze findet. Die
// Methode soll außerdem die erste gefundene Position (Zeile und
// Spalte in Java-Zählweise, also 1. Reihe bzw. 1. Platz = 0) am
// Bildschirm ausgeben.
public class Main {
public static void main
(String[] args
) {
int [][] kino = {
{1 , 1 , 0 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1} ,
{1 , 1 , 1 , 0 , 0 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1} ,
{1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1} ,
{1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1} ,
{1 , 1 , 1 , 1 , 0 , 0 , 1 , 1 , 1 , 1 , 1 , 1 , 1} ,
{0 , 0 , 0 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 0} ,
{0 , 0 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 0} ,
{0 , 0 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 0 , 0 , 0 , 0} ,
{0 , 0 , 0 , 0 , 1 , 1 , 1 , 1 , 0 , 0 , 0 , 0 , 0}
};
//System.out.println(seatTest(2,kino));
boolean check =seatTest(3,kino);
}
public static boolean seatTest (int howManySeats, int[][] data){
boolean seats = false;
boolean problem = false;
// howManySeats -1, because input is easier for humans to read
howManySeats--;
for(int row=0; row< data.length; row++){
for(int col=0; col<data[row].length; col++){
// if free seat is detected -> check if the seat int howManySeats next to it is also free
if(data[row][col]==0){
// check if there are enough seats or row is too short
if(col+howManySeats>=data[row].length){
//System.out.println("not enough seats... skipping");
} else {
// seat howManySeats next to it is also free so check if seats in between are free
if (data[row][col + howManySeats] == 0) {
for (int i = col + howManySeats; i > col; i--) {
// one seat is not free boolean problem is true
if (data[row][i] == 1) {
problem = true;
}
}
// if there is no problem a free space was found
// seats = true could be replaced with return so only first gap will be put out
if (problem == false) {
System.
out.
println(howManySeats
+ 1 + " free seats found at Row " + row
+ " and Column " + col
+ "!");
seats = true;
}
}
}
// raise col to not check gaps twice
col = col + howManySeats+1;
}
}
}
return seats;
}