-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathairplane_seating.java
97 lines (88 loc) · 2.26 KB
/
airplane_seating.java
1
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import java.util.*;
public class airplane_seating {
public static void main(String[] args) {
List<int[]> seating = new ArrayList<>();
seating.add(new int[] { 3, 2 });
seating.add(new int[] { 4, 3 });
seating.add(new int[] { 2, 3 });
seating.add(new int[] { 3, 4 });
int nPassengers = 30;
Queue<int[]> q = new LinkedList<>();
List<int[][]> ls = new LinkedList<>();
getOrder(q, seating);
tagSeats(ls, seating);
for (int[][] i : ls) {
for (int j[] : i) {
for (int k : j) {
System.out.print(k + " ");
}
System.out.println();
}
System.out.println();
}
printSol(q, ls, nPassengers);
}
private static void printSol(Queue<int[]> q, List<int[][]> ls, int nPassengers) {
int z = 1, i = 1;
while (i <= nPassengers) {
Queue<int[]> o = new LinkedList<>(q);
while (!o.isEmpty()) {
int x[] = o.poll();
if (ls.get(x[0])[x[1]][x[2]] == z) {
System.out.println("Seat Number " + i + " is placed at Compartment No. " + (x[0] + 1) + ", Row No. "
+ (x[1] + 1) + ", Col No. " + (x[2] + 1));
i++;
if (i > nPassengers)
return;
}
}
z++;
if (z == 3)
z = 0;
}
}
private static void tagSeats(List<int[][]> ls, List<int[]> seating) {
for (int[] x : seating) {
ls.add(new int[x[1]][x[0]]);
}
tagWindow(ls, seating);
tagAisle(ls, seating);
}
private static void tagWindow(List<int[][]> ls, List<int[]> seating) {
int[][] x = ls.get(0);
for (int i = 0; i < x.length; i++) {
x[i][0] = 2;
}
int y = seating.size() - 1;
x = ls.get(y);
for (int i = 0; i < x.length; i++) {
x[i][x[0].length - 1] = 2;
}
}
private static void tagAisle(List<int[][]> ls, List<int[]> seating) {
for (int[][] x : ls) {
int r = x.length, c = x[0].length;
for (int i = 0; i < r; i++) {
if (x[i][0] == 0)
x[i][0] = 1;
if (x[i][c - 1] == 0)
x[i][c - 1] = 1;
}
}
}
private static void getOrder(Queue<int[]> q, List<int[]> seating) {
int stop = seating.get(0)[1];
for (int[] x : seating)
stop = Math.max(x[1], stop);
for (int i = 0; i < stop; i++) {
for (int j = 0; j < seating.size(); j++) {
int r = seating.get(j)[1], c = seating.get(j)[0];
if (i >= r)
continue;
for (int k = 0; k < c; k++) {
q.add(new int[] { j, i, k });
}
}
}
}
}