-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFAR.java
More file actions
787 lines (704 loc) · 20.9 KB
/
FAR.java
File metadata and controls
787 lines (704 loc) · 20.9 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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Arrays;
public class FAR
{
static MapNode[][] map;
static int numRows, numCols;
static int noWalk;
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
numRows = Integer.parseInt(input.nextLine());
numCols = Integer.parseInt(input.nextLine());
map = new MapNode[numRows + 2][numCols + 2];
noWalk = 0;
int currRow = 0;
while(input.hasNext())
{
String line = input.nextLine();
String[] spots = line.split(",");
for(int i = 0; i < spots.length; i++)
{
map[currRow + 1][i + 1] = new MapNode(spots[i].equals("0"), currRow + 1, i + 1);
if(!spots[i].equals("0"))
{
noWalk++;
}
}
currRow++;
}
addBorder();
addOneWayStreets();
printMap();
ensureConnectivity();
verifyMap();
printMap();
}
public static boolean verifyMap()
{
// printMap();
// System.out.println("Coorect Answer ==> " + (numRows * numCols - noWalk));
for(int row = 1; row <= numRows; row++)
{
for(int col = 1; col <= numCols; col++)
{
// System.out.print(row + " " + col + " ==> ");
if(map[row][col].canWalk)
{
boolean[][] seen = new boolean[numRows + 2][numCols + 2];
seen[row][col] = true;
DFS(row, col, seen);
for(int r = 1; r <= numRows; r++)
{
for(int c = 1; c <= numCols; c++)
{
if(seen[r][c] == false && map[r][c].canWalk)
{
// System.out.println(r + " " + c);
if(isNextTo(row, col, r, c))
{
DIRECTION d1 = DIRECTION.directionFromVector(row - r, col - c);
DIRECTION d2 = DIRECTION.directionFromVector(r - row, c - col);
map[row][col].changeEdge(d2, EDGE_TYPE.BOTH);
map[r][c].changeEdge(d1, EDGE_TYPE.BOTH);
return verifyMap();
}
}
}
}
}
else
{
// System.out.println("Can't walk here");
}
}
}
return true;
}
public static boolean isNextTo(int row1, int col1, int row2, int col2)
{
if(row1 == row2 && Math.abs(col1 - col2) == 1)
{
return true;
}
if(col1 == col2 && Math.abs(row1 - row2) == 1)
{
return true;
}
return false;
}
public static int DFS(int row, int col, boolean[][] seen)
{
if(!map[row][col].canWalk)
{
return 0;
}
else
{
int count = 1;
for(int i = 0; i < DIRECTION.numDirections; i++)
{
if(map[row][col].edges[i] == EDGE_TYPE.OUT || map[row][col].edges[i] == EDGE_TYPE.BOTH)
{
int[] vector = DIRECTION.directionToVector[i];
if(!seen[row + vector[0]][col + vector[1]])
{
seen[row + vector[0]][col + vector[1]] = true;
count += DFS(row + vector[0], col + vector[1], seen);
}
}
}
return count;
}
}
public static void ensureConnectivity()
{
ArrayList<Object[]> tunnels = new ArrayList<Object[]>();
ArrayList<Object[]> sinksAndSources = new ArrayList<Object[]>();
for(int row = 1; row <= numRows; row++)
{
for(int col = 1; col <= numCols; col++)
{
if(isTunnel(row, col))
{
tunnels.add(tunnelFix(row, col));
}
else if(isSink(row, col) || isSource(row, col))
{
ArrayList<Object[]> fixes = sinkOrSourceFix(row, col);
for(Object[] fix: fixes)
{
sinksAndSources.add(fix);
}
}
}
}
for(int i = 0; i < tunnels.size(); i++)
{
applyTunnelFix(tunnels.get(i));
}
System.out.println();
for(int i = 0; i < sinksAndSources.size(); i++)
{
applySinkOrSourceFix(sinksAndSources.get(i));
}
for(int row = 1; row <= numRows; row++)
{
for(int col = 1; col <= numCols; col++)
{
checkAndFixIslands(row, col);
}
}
}
public static void checkAndFixIslands(int row, int col)
{
MapNode curr = map[row][col];
if(curr.incomingEdges + curr.outgoingEdges == 1)
{
for(int i = DIRECTION.minCardinal; i < DIRECTION.maxCardinal; i++)
{
if(curr.edges[i] != EDGE_TYPE.NONE)
{
curr.changeEdge(DIRECTION.directionFromInt(i), EDGE_TYPE.BOTH);
int[] vector = DIRECTION.directionToVector[i];
map[row + vector[0]][col + vector[1]].changeEdgeTo(row, col, EDGE_TYPE.BOTH);
}
}
}
}
public static Object[] tunnelFix(int row, int col)
{
MapNode curr = map[row][col];
Object[] tuple = new Object[4];
tuple[0] = new Integer(row);
tuple[1] = new Integer(col);
int t = 2;
for(int i = DIRECTION.minCardinal; i < DIRECTION.maxCardinal; i++)
{
if(curr.edges[i] == EDGE_TYPE.OUT || curr.edges[i] == EDGE_TYPE.IN)
{
tuple[t] = DIRECTION.directionFromInt(i);
t++;
}
}
if(t != 4)
{
System.out.println("Error creating tunnel fix for: " + row + " " + col);
return null;
}
return tuple;
}
public static void applyTunnelFix(Object[] fix)
{
// for(int i = 0; i < 4; i++)
// {
// System.out.print(fix[i] + " ");
// }
// System.out.println();
int row = (Integer)fix[0];
int col = (Integer)fix[1];
MapNode curr = map[row][col];
// first edge
DIRECTION directionOne = (DIRECTION)fix[2];
int[] vectorOne = DIRECTION.directionToVector[directionOne.getValue()];
curr.edges[directionOne.getValue()] = EDGE_TYPE.BOTH;
map[row + vectorOne[0]][col + vectorOne[1]].changeEdgeTo(row, col, EDGE_TYPE.BOTH);
// second edge
DIRECTION directionTwo = (DIRECTION)fix[3];
int[] vectorTwo = DIRECTION.directionToVector[directionTwo.getValue()];
curr.edges[directionTwo.getValue()] = EDGE_TYPE.BOTH;
map[row + vectorTwo[0]][col + vectorTwo[1]].changeEdgeTo(row, col, EDGE_TYPE.BOTH);
}
public static ArrayList<Object[]> sinkOrSourceFix(int row, int col)
{
MapNode curr = map[row][col];
ArrayList<Object[]> fixes = new ArrayList<Object[]>();
for(int dR = -1; dR < 2; dR += 2)
{
for(int dC = -1; dC < 2; dC += 2)
{
if(map[row + dR][col + dC].canWalk == false)
{
continue;
}
else if(isSink(row + dR, col + dC) || isSource(row + dR, col + dC))
{
continue;
}
else if(checkDiagonalMove(row, col, row + dR, col + dC))
{
Object[] tuple = new Object[4];
tuple[0] = new Integer(row);
tuple[1] = new Integer(col);
tuple[2] = DIRECTION.directionFromVector(dR, dC);
tuple[3] = isSource(row, col) ? EDGE_TYPE.IN : EDGE_TYPE.OUT;
fixes.add(tuple);
return fixes;
}
}
}
for(int dR = -1; dR < 2; dR++)
{
for(int dC = -1; dC < 2; dC++)
{
if(Math.abs(dR) == Math.abs(dC))
{
continue;
}
else if(map[row + dR][col + dC].canWalk == false)
{
continue;
}
else if(isSink(row + dR, col + dC) || isSource(row + dR, col + dC))
{
continue;
}
else
{
Object[] tuple = new Object[4];
tuple[0] = new Integer(row);
tuple[1] = new Integer(col);
tuple[2] = DIRECTION.directionFromVector(dR, dC);
tuple[3] = isSource(row, col) ? EDGE_TYPE.IN : EDGE_TYPE.OUT;
fixes.add(tuple);
return fixes;
}
}
}
return null;
}
public static void applySinkOrSourceFix(Object[] fix)
{
// for(int i = 0; i < 4; i++)
// {
// System.out.print(fix[i] + " ");
// }
// System.out.println();
int row = (Integer)fix[0];
int col = (Integer)fix[1];
MapNode curr = map[row][col];
DIRECTION dir = (DIRECTION)fix[2];
EDGE_TYPE type = (EDGE_TYPE)fix[3];
if(curr.edges[dir.getValue()] != EDGE_TYPE.NONE)
{
curr.changeEdge(dir, EDGE_TYPE.BOTH);
int[] vector = DIRECTION.directionToVector[dir.getValue()];
map[row + vector[0]][col + vector[1]].changeEdgeTo(row, col, EDGE_TYPE.BOTH);
}
else
{
curr.changeEdge(dir, type);
int[] vector = DIRECTION.directionToVector[dir.getValue()];
map[row + vector[0]][col + vector[1]].changeEdgeTo(row, col, (type == EDGE_TYPE.IN) ? EDGE_TYPE.OUT : EDGE_TYPE.IN);
}
}
public static void addOneWayStreets()
{
MapNode curr = null;
MapNode toLeft = null, toRight = null;
MapNode above = null, below = null;
for(int row = 1; row <= numRows; row++)
{
for(int col = 1; col <= numCols; col++)
{
curr = map[row][col];
if(curr.canWalk == false)
{
continue;
}
if(col != 1)
{
toLeft = map[row][col - 1];
}
if(col != numCols)
{
toRight = map[row][col + 1];
}
if(row != 1)
{
above = map[row - 1][col];
}
if(row != numRows)
{
below = map[row + 1][col];
}
if(row % 2 == 0)
{
if(toLeft != null && toLeft.canWalk)
{
toLeft.changeEdge(DIRECTION.RIGHT, EDGE_TYPE.OUT);
curr.changeEdge(DIRECTION.LEFT, EDGE_TYPE.IN);
}
if(toRight != null && toRight.canWalk)
{
curr.changeEdge(DIRECTION.RIGHT, EDGE_TYPE.OUT);
toRight.changeEdge(DIRECTION.LEFT, EDGE_TYPE.IN);
}
}
else
{
if(toLeft != null && toLeft.canWalk)
{
toLeft.changeEdge(DIRECTION.RIGHT, EDGE_TYPE.IN);
curr.changeEdge(DIRECTION.LEFT, EDGE_TYPE.OUT);
}
if(toRight != null && toRight.canWalk)
{
curr.changeEdge(DIRECTION.RIGHT, EDGE_TYPE.IN);
toRight.changeEdge(DIRECTION.LEFT, EDGE_TYPE.OUT);
}
}
if(col % 2 == 0)
{
if(above != null && above.canWalk)
{
above.changeEdge(DIRECTION.DOWN, EDGE_TYPE.OUT);
curr.changeEdge(DIRECTION.UP, EDGE_TYPE.IN);
}
if(below != null && below.canWalk)
{
curr.changeEdge(DIRECTION.DOWN, EDGE_TYPE.OUT);
below.changeEdge(DIRECTION.UP, EDGE_TYPE.IN);
}
}
else
{
if(above != null && above.canWalk)
{
above.changeEdge(DIRECTION.DOWN, EDGE_TYPE.IN);
curr.changeEdge(DIRECTION.UP, EDGE_TYPE.OUT);
}
if(below != null && below.canWalk)
{
curr.changeEdge(DIRECTION.DOWN, EDGE_TYPE.IN);
below.changeEdge(DIRECTION.UP, EDGE_TYPE.OUT);
}
}
curr = null;
toLeft = null;
toRight = null;
above = null;
below = null;
}
}
}
public static void addBorder() {
for(int i = 0; i < numRows + 2; i++)
{
if(i == 0 || i == numRows + 2 - 1)
{
for(int j = 0; j < numCols + 2; j++)
{
map[i][j] = new MapNode(false, -1, -1);
}
}
else
{
map[i][0] = new MapNode(false, -1, -1);
map[i][numCols + 2 - 1] = new MapNode(false, -1, -1);
}
}
}
public static boolean isTunnel(int row, int col)
{
MapNode curr = map[row][col];
if(curr.outgoingEdges == 1 && curr.incomingEdges == 1)
{
return true;
}
else
{
return false;
}
}
public static boolean isCorner(int x, int y)
{
for(int i = -1; i < 2; i += 2)
{
for(int j = -1; j < 2; j++)
{
if(map[x + i][y].canWalk == false && map[x][y + j].canWalk == false)
{
return true;
}
}
}
return false;
}
public static boolean isSink(int x, int y)
{
if(isCorner(x, y))
{
return map[x][y].outgoingEdges == 0 && map[x][y].incomingEdges == 2;
}
return false;
}
public static boolean isSource(int x, int y)
{
if(isCorner(x, y))
{
return map[x][y].outgoingEdges == 2 && map[x][y].incomingEdges == 0;
}
return false;
}
public static boolean checkDiagonalMove(int row1, int col1, int row2, int col2)
{
if(col1 > col2)
{
if(!map[row1][col1 - 1].canWalk || !map[row2][col2 + 1].canWalk)
{
return false;
}
}
else if(col1 < col2)
{
if(!map[row1][col1 + 1].canWalk || !map[row2][col2 - 1].canWalk)
{
return false;
}
}
return true;
}
public static void printMap()
{
for(int row = 0; row < numRows + 2; row++)
{
for(int col = 0; col < numCols + 2; col++)
{
System.out.print(map[row][col]);
}
System.out.println();
}
}
}
enum DIRECTION
{
UP(0),
DOWN(1),
LEFT(2),
RIGHT(3),
UP_LEFT(4),
UP_RIGHT(5),
DOWN_LEFT(6),
DOWN_RIGHT(7);
public static int[][] directionToVector = {
{-1, 0},
{1, 0},
{0, -1},
{0, 1},
{-1, -1},
{-1, 1},
{1, -1},
{1, 1}
};
private final int value;
public static final int numDirections = 8;
public static final int minCardinal = 0;
public static final int maxCardinal = 4;
private DIRECTION(int value)
{
this.value = value;
}
public static DIRECTION directionFromInt(int d)
{
switch(d)
{
case 0:
return UP;
case 1:
return DOWN;
case 2:
return LEFT;
case 3:
return RIGHT;
case 4:
return UP_LEFT;
case 5:
return UP_RIGHT;
case 6:
return DOWN_LEFT;
case 7:
return DOWN_RIGHT;
}
return null;
}
public static String stringFromInt(int d)
{
switch(d)
{
case 0:
return "UP";
case 1:
return "DOWN";
case 2:
return "LEFT";
case 3:
return "RIGHT";
case 4:
return "UP_LEFT";
case 5:
return "UP_RIGHT";
case 6:
return "DOWN_LEFT";
case 7:
return "DOWN_RIGHT";
}
return null;
}
public static DIRECTION directionFromVector(int x, int y)
{
for(int i = 0; i < directionToVector.length; i++)
{
if(x == directionToVector[i][0] && y == directionToVector[i][1])
{
return directionFromInt(i);
}
}
return null;
}
public int getValue()
{
return value;
}
}
enum EDGE_TYPE
{
NONE(0, "N"),
IN(1, "I"),
OUT(2, "O"),
BOTH(3, "B");
private final int value;
private final String sValue;
private EDGE_TYPE(int value, String sValue)
{
this.value = value;
this.sValue = sValue;
}
public int getValue()
{
return value;
}
public String getsValue()
{
return sValue;
}
}
class MapNode
{
public boolean canWalk;
public EDGE_TYPE[] edges;
public int r, c, incomingEdges, outgoingEdges;
public MapNode(boolean cW, int r, int c)
{
canWalk = cW;
edges = new EDGE_TYPE[DIRECTION.numDirections];
changeAllEdges(EDGE_TYPE.NONE);
this.r = r;
this.c = c;
}
public void changeEdge(DIRECTION which, EDGE_TYPE type)
{
edges[which.getValue()] = type;
countEdges();
}
public void changeAllEdges(EDGE_TYPE type)
{
for(int i = 0; i < DIRECTION.numDirections; i++)
{
edges[i] = type;
}
countEdges();
}
public void changeEdgeTo(int oRow, int oCol, EDGE_TYPE type)
{
if(r == oRow)
{
if(c < oCol)
{
edges[DIRECTION.RIGHT.getValue()] = type;
}
else if(c > oCol)
{
edges[DIRECTION.LEFT.getValue()] = type;
}
}
if(c == oCol)
{
if(r < oRow)
{
edges[DIRECTION.DOWN.getValue()] = type;
}
else if(r > oRow)
{
edges[DIRECTION.UP.getValue()] = type;
}
}
if(r > oRow)
{
if(c < oCol)
{
edges[DIRECTION.UP_RIGHT.getValue()] = type;
}
else if(c > oCol)
{
edges[DIRECTION.UP_LEFT.getValue()] = type;
}
}
if(r < oRow)
{
if(c < oCol)
{
edges[DIRECTION.DOWN_RIGHT.getValue()] = type;
}
else if(c > oCol)
{
edges[DIRECTION.DOWN_LEFT.getValue()] = type;
}
}
countEdges();
}
private void countEdges()
{
incomingEdges = 0;
outgoingEdges = 0;
for(int i = 0; i < DIRECTION.numDirections; i++)
{
if(edges[i] == EDGE_TYPE.BOTH)
{
incomingEdges++;
outgoingEdges++;
}
else if(edges[i] == EDGE_TYPE.IN)
{
incomingEdges++;
}
else if(edges[i] == EDGE_TYPE.OUT)
{
outgoingEdges++;
}
}
}
public String toString()
{
String ret = "----------------- ";
if(canWalk)
{
ret = "T,";
}
else
{
return ret;
}
for(int i = 0; i < DIRECTION.numDirections; i++)
{
ret += edges[i].getsValue();
if(i != DIRECTION.numDirections - 1)
{
ret += ",";
}
}
ret += " ";
return ret;
}
}