-
Notifications
You must be signed in to change notification settings - Fork 0
/
AncientMessages.java
227 lines (220 loc) · 5.65 KB
/
AncientMessages.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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import java.util.* ;
import java.io.BufferedReader ;
import java.io.InputStreamReader ;
/*
Bugs:
#1 : The conversion from hexa Matrix to binary Matrix doesn't work properly
->Solved: S[i][j] maps to M[i][4*j] not M[i][j].
#2 : nh = 16 for the input
4 4
f0f0
f0f0
f0f0
f0f0
0 0
Should be only 2.
-> r to r_new and c to c_new
-> the cell checked is assigned nh.
-> The bfs isn't working properly.
-> The condition was checking M[r][c] instead of M[r_new][c_new]
Solved.
#3 : IndexOutOfBoundsException: At the instantiation of the HashSets of heiroglyphs ArrayList
Solved: instead of heiroglyphs.set(i,new HashSet<Integer>()), did heiroglyphs.add(new HashSet<Integer>())
#4 : IndexOutOfBoundsException: Instead of "nh-2" did "nh"
Solved.
#5 : Wrong answer for the input :
4 4
0f0f
ff00
0000
f00f
0 0
-> A layer of padding for the input is needed. So that no heiroglyph closes a white area unintentionally.
-> i was from 0 to n-1, now 1 to n. j not changed. Just from M[i][4*j+k] to M[i][4*j+k+1], to shift all columns to right
# 6 : Recognizing only 7 inputs out of 10 inputs for uDebug test case 2.
-> The condition s.charAt(0) !=0 && s.charAt(2)!=0, would be very wrong. Changed it. So it recognized 200 50 as EOF.
*/
class pair
{
int first,second ;
pair(int f,int s)
{
first = f ;
second = s ;
}
}
public class AncientMessages
{
private static final boolean debug = false ;
private static final boolean debug2 = false ;
static char[] symbols = {'0','W','A','K','J','S','D'} ;//In accordance to the number of corresponding white spaces
static int dr[] = {-1,0,1,0} ;
static int dc[] = {0,1,0,-1} ;
static void bfs_floodfill_black(int r,int c,int[][] matrix,int nh)//nh = number of heiroglyphs, starts from 2
{//for the heiroglyphs
if(debug)
System.out.println("Into ["+r+"]["+c+"]") ;
if(matrix[r][c]!=1)
return ;
matrix[r][c] = nh ;
int rows = matrix.length, cols = matrix[0].length ;
Queue<pair> Q = new ArrayDeque<pair>() ;
Q.add(new pair(r,c)) ;
while(!Q.isEmpty())
{
if(debug)
System.out.println("Inside the while loop.") ;
pair p = Q.poll() ;
r = p.first ;
c = p.second ;
for(int i=0;i<4;i++)
{//finding the nearby cells
int r_new = r+dr[i] ;
int c_new = c+dc[i] ;
if(r_new>=0 && r_new <rows && c_new>=0 && c_new <cols && matrix[r_new][c_new]==1)
{//if not out of the input
matrix[r_new][c_new]=nh ;
if(debug)
{
System.out.println(" Checking : ["+r_new+"]["+c_new+"]") ;
}
Q.add(new pair(r_new,c_new)) ;
}
}
}
}
static void bfs_floodfill_white(int r,int c,int[][] matrix, int nw,List<HashSet<Integer>> heiroglyphs)//nw = number of whitespaces(-ve)
{//for the white spaces
if(matrix[r][c]!=0)
return ;
int rows = matrix.length, cols = matrix[0].length ;
Queue<pair> Q = new ArrayDeque<pair>() ;
Q.add(new pair(r,c)) ;
while(!Q.isEmpty())
{
pair p = Q.poll() ;
r = p.first ;
c = p.second ;
for(int i=0;i<4;i++)
{
int r_new = r+dr[i] ;
int c_new = c+dc[i] ;
if(r_new>=0 && r_new <rows && c_new>=0 && c_new <cols)
{
if(matrix[r_new][c_new] == 0)
{
matrix[r_new][c_new] = nw ;
Q.add(new pair(r_new,c_new)) ;
}
else if(matrix[r_new][c_new]>0)
{//a heiroglyph is found
heiroglyphs.get(matrix[r_new][c_new]-2).add(nw) ;
}
}
}
}
}
static void ArrayPrinter(int[][] M)
{
for(int i=0;i<M.length;i++)
{
for(int j=0;j<M[0].length;j++)
{
System.out.print(M[i][j]) ;
}
System.out.println() ;
}
}
public static void main(String args[]) throws Exception
{
BufferedReader bro = new BufferedReader(new InputStreamReader(System.in)) ;
int cases = 0;
for(String s = bro.readLine();;s = bro.readLine())
{//Input reading
cases++ ;
String[] spl = s.split(" ") ;
if(Integer.parseInt(spl[0])==0 && Integer.parseInt(spl[1])==0)
break ;
int n = Integer.parseInt(spl[0]), m = Integer.parseInt(spl[1]) ;
if(debug2) System.out.println("n :"+n+" m :"+m) ;
int[][] M = new int[n+2][4*m+2] ;//+2 for the padding
// String S = bro.readLine() ;
for(int i=1;i<=n;i++)
{
String S = bro.readLine() ;
for(int j = 0;j<m;j++)
{
char ch = S.charAt(j) ;
int val = Character.isLetter(ch)? ch-'a'+10:ch-'0' ;
if(debug) System.out.println(ch+" :: "+val) ;
for(int k=0,b=8;k<4;k++,b>>=1)
{
M[i][4*j+k+1] = (b & val)>0?1:0 ;
}
}
}
if(debug)
{
System.out.println("Original :") ;
ArrayPrinter(M) ;
}
int nh = 2 ;
for(int i=0;i<M.length;i++)
{
for(int j=0;j<M[i].length;j++)
{
if(M[i][j]==1)
{
bfs_floodfill_black(i,j,M,nh) ;
nh++ ;
}
}
}
if(debug)
{
System.out.println("No. of heiroglyphs :"+(nh-2)) ;
ArrayPrinter(M) ;
}
List<HashSet<Integer>> heiroglyphs = new ArrayList<HashSet<Integer>>() ;
for(int i=0;i<nh-2;i++)
{//Instantiation
// heiroglyphs.set(i,new HashSet<Integer>()) ;
heiroglyphs.add(new HashSet<Integer>()) ;
}
int nw = 0 ;
for(int i=0;i<M.length;i++)
{
for(int j=0;j<M[i].length;j++)
{
if(M[i][j]==0)
{
bfs_floodfill_white(i,j,M,-1*(nw+1),heiroglyphs) ;
nw++ ;
}
}
}
if(debug)
{
System.out.println("nw :"+(nw)) ;
ArrayPrinter(M) ;
}
int[] size = new int[nh-2] ;
for(int i=0;i<nh-2;i++)
{
size[i] = heiroglyphs.get(i).size() ;
}
// Arrays.sort(size) ;
char[] Heiro = new char[nh-2] ;
System.out.print("Case "+cases+": ") ;
for(int i=0;i<nh-2;i++)
{
// System.out.print(symbols[size[i]]) ;
Heiro[i] = symbols[size[i]] ;
}
Arrays.sort(Heiro) ;
for(char i : Heiro)
System.out.print(i) ;
System.out.println() ;
}
}
}