-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathattemptNavi.java
More file actions
174 lines (134 loc) · 6.35 KB
/
Copy pathattemptNavi.java
File metadata and controls
174 lines (134 loc) · 6.35 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import java.util.*;
import java.io.*;
public class attemptNavi {
private static int stanzas;
public static Stack <Integer> seen;
public static ArrayList<ArrayList<Integer>> inputs;
public static HashMap<Integer, Integer> shownup;
public static void main(String[] args) {
Scanner s = new Scanner(new InputStreamReader(System.in));
stanzas = s.nextInt();
for (int i = 0; i < stanzas; i++) {
//System.out.println("start: " + i);
int colors = s.nextInt();
int beams = s.nextInt();
boolean bad = false;
inputs = new ArrayList<ArrayList<Integer>>();
HashMap<Integer, Integer> firstBeamOnWhichColorSeen = new HashMap<Integer, Integer>();
ArrayList<HashMap<Integer, Boolean> > colorsPresentOnBeams = new ArrayList<HashMap<Integer, Boolean>>();
HashMap<Integer /* Beam */, HashMap<Integer /* color */, ArrayList<Integer>>> colorDepsForEachBeam =
new HashMap<Integer /* Beam */, HashMap<Integer /* color */, ArrayList<Integer>>>();
//read in inputs
for (int j = 0; j < beams; j++) {
inputs.add(new ArrayList<Integer>());
ArrayList<Integer> beadsOnBeam = inputs.get(j);
colorsPresentOnBeams.add(new HashMap<Integer, Boolean>());
int colorsInBeam = s.nextInt();
ArrayList<Integer> colorsOrderPerBeam = new ArrayList<Integer>();
for (int l = 0; l < colorsInBeam; l++) {
int color = s.nextInt();
beadsOnBeam.add(color); /* Recorded on inputs */
if (!colorsOrderPerBeam.contains(color)){
colorsOrderPerBeam.add(color);
}
if(!colorsPresentOnBeams.get(j).containsKey(color)) {
// Only add the sequence for the 1st time we see the color on the beam, not the second time
colorsPresentOnBeams.get(j).put(color, true);
ArrayList<Integer> newTempAL = new ArrayList<Integer>();
newTempAL.addAll(colorsOrderPerBeam);
colorDepsForEachBeam.get(j).put(color, newTempAL);
}
if (!firstBeamOnWhichColorSeen.containsKey(color)) {
firstBeamOnWhichColorSeen.put(color, j);
}
}
}
/* Bracelet continuity between beams (is it closed or not) */
for (int j = 0; j < beams; j++) {
ArrayList<Integer> beadsOnBeam = inputs.get(j);
for (int l = 0; l < beadsOnBeam.size(); l++) {
int color = beadsOnBeam.get(l);
if (j == 0) {
continue;
}
if (firstBeamOnWhichColorSeen.get(color) < j) {
if (!colorsPresentOnBeams.get(j - 1).containsKey(color)) {
/* broken bracelet case */
bad = true;
break;
}
}
}
if (bad) {
break;
}
}
if (bad == true) {
System.out.println("NO");
continue;
}
/* Check if the bracelets are concentric inside a single beam
If its something likes 1 - 2 - 1 - 2 then its bad - colors interset on the same beam
if its 1 - 2 - 2 - 1 then it is ok
We keep a stack to record all the open colors.
*/
//now using stack to check for intersection between objects if goes such as 1 - 2 - 1 - 2
//this is first case of this type (second dealing with edge case which avoids this comes after)
for (int j = 0; j < beams; j++) {
ArrayList <Integer> currentBeam = inputs.get(j);
seen = new Stack <Integer>();
shownup = new HashMap<Integer, Integer>();
for (int l = 0; l < currentBeam.size(); l++) {
int currentColor = currentBeam.get(l);
if (!shownup.containsKey(currentColor)) {
/* First time we see this color. So mark the color as seen and push it on the stack */
shownup.put(currentColor, 0);
seen.push(currentColor);
continue;
} else {
/* Second time we see this color, so pop it from the stack.
Also see it the top the stack is the same color, if not we have an intersecting bracelet */
int temp = seen.pop();
if (temp != currentColor) {
bad = true;
break;
}
}
}
if (bad) {
break;
}
}
if (bad == true) {
System.out.println("NO");
continue;
}
/* Cross check dependency order across beams
Detect this case and reject it
1 - 1 - 2 - 2
2 - 2 - 1 - 1
HashMap<Integer, Integer> firstBeamOnWhichColorSeen = new HashMap<Integer, Integer>();
ArrayList<HashMap<Integer, Boolean> > colorsPresentOnBeams = new ArrayList<HashMap<Integer, Boolean>>();
*/
//now for the remaning edge case dealing with the intersection if it goes 1 - 2 - 2 - 1
//use smth to keep track of sequence across the objects detected under light beams
for (int j = 0; j < beams; j++) {
ArrayList<Integer> beadsOnBeam = inputs.get(j);
for (int l = 0; l < beadsOnBeam.size(); l++) {
int color = beadsOnBeam.get(l);
}
}
if (bad == true) {
System.out.println("NO");
continue;
}
/**************** */
if (bad) {
System.out.println("NO");
}
else {
System.out.println("YES");
}
}
}
}