Cinema
# Topic
Multidimensional array
# Problem
Cinema
https://hyperskill.org/learn/step/1930
The cinema has n rows, each row consists of m seats (n and m do not exceed 20). A two-dimensional matrix stores the information on the sold tickets: the number 1 means that the ticket for this place is already sold, and the number 0 means that the place is available. You want to buy k tickets to neighboring seats in the same row. Find whether it can be done.
Input data format
On the input, the program gets the number of n rows and m seats. Then, there are n lines, each containing m numbers (0 or 1) separated by spaces. The last line contains the number k.
Output data format
The program should output the number of the row with k consecutive available seats. If there are several rows with k available seats, output the first row with these seats. If there is no such a row, output the number 0.
Sample Input 1:
3 4
0 1 0 1
1 1 0 1
1 0 0 1
2
2
3
4
5
Sample Output 1:
3
Sample Input 2:
3 3
0 1 0
1 0 0
1 1 1
3
2
3
4
5
Sample Output 2:
0
Sample Input 3:
2 4
1 1 0 0
0 0 1 1
4
2
3
4
Sample Output 3:
0
# Hint & Explain
If you're using a counter, remember to reset it when going from row to row.
# Solution
# My solution
import java.util.Scanner;
class Main {
public static void main(String[] args) {
// put your code here
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt(); // rows
int m = scanner.nextInt(); // seats
int[][] matrixOfSeats = new int[n][m]; // matrix
for (int i = 0; i < matrixOfSeats.length; i++) {
for (int j = 0; j < matrixOfSeats[i].length; j++) {
matrixOfSeats[i][j] = scanner.nextInt();
}
}
int k = scanner.nextInt(); // target
int counter;
int temp = 0;
int targetOfRow = 0;
boolean stop = false;
for (int i = 0; i < matrixOfSeats.length; i++) {
counter = 0;
for (int j = 0; j < matrixOfSeats[i].length; j++) {
if (matrixOfSeats[i][j] == 0) {
counter++;
temp = counter;
if (temp >= k) {
targetOfRow = i + 1;
stop = true;
break;
}
} else {
counter = 0;
}
}
if (stop) {
break;
}
}
System.out.println(targetOfRow);
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# Other solution 1 (Pretty code!!!)
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int m = scanner.nextInt();
int[][] table = new int[n][m];
int number;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
number = scanner.nextInt();
table[i][j] = number;
}
}
int k = scanner.nextInt();
int counter;
int i = 0;
boolean available = false;
LOOP:
for (; i < n; i++) {
counter = 0;
for (int j = 0; j < m; j++) {
if (table[i][j] == 0) {
counter++;
} else {
counter = 0;
}
if (counter == k) {
available = true;
break LOOP;
}
}
}
if (available) {
System.out.println(i + 1);
} else {
System.out.println(0);
}
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# Other solution 2
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int row = sc.nextInt();
int col = sc.nextInt();
int[][] seats = new int[row][col];
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
seats[i][j] = sc.nextInt();
}
}
int k = sc.nextInt();
int count = 0;
int maxCount = 0;
for (int i = 0; i < row; i++) {
count = 0;
for (int j = 0; j < col; j++) {
if (seats[i][j] == 0) {
count++;
} else {
count = 0;
}
if (count > maxCount) {
maxCount = count;
}
}
if (maxCount >= k) {
System.out.println(i + 1);
return;
}
}
System.out.println(0);
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# Other solution 3
import java.util.Scanner;
class Main {
public static String[] seats;
public static int requiredTickets = 0;
public static void main(String[] args) {
// put your code here
readInput();
for (int i = 0; i < seats.length; i++) {
if (seats[i].contains("0".repeat(requiredTickets))) {
System.out.println(i + 1);
return;
}
}
System.out.println(0);
}
public static void readInput() {
Scanner scanner = new Scanner(System.in);
String buf = scanner.nextLine();
String[] bufSplitted = buf.split(" ");
int rows = Integer.parseInt(bufSplitted[0]);
seats = new String[rows];
for (int i = 0; i < rows; i++) {
seats[i] = scanner.nextLine().replace(" ", "");
}
requiredTickets = scanner.nextInt();
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31