-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThereIsNoSpoonEpisode1.java
117 lines (99 loc) · 2.31 KB
/
ThereIsNoSpoonEpisode1.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
/*
* Author: Robin Péricé (2018)
*/
package solutions.practice.classicPuzzles.medium;
import java.util.Scanner;
import common.ISolution;
public class ThereIsNoSpoonEpisode1 implements ISolution {
public static class Case {
boolean isNode;
int x;
int y;
public Case(final int x, final int y, final boolean isNode) {
this.x = x;
this.y = y;
this.isNode = isNode;
}
}
static int height;
static int width;
public String compute(final Case c, final Case[][] grille) {
if (c.isNode) {
final StringBuilder b = new StringBuilder();
b.append(c.x + " " + c.y);
if (c.x + 1 >= width || width == 1) {
b.append(" -1 -1");
} else {
int i = 1;
boolean voisin = false;
while (c.x + i < width) {
final Case voisinDroite = grille[c.y][c.x + i];
if (voisinDroite.isNode) {
b.append(" " + voisinDroite.x + " " + voisinDroite.y);
voisin = true;
break;
}
i++;
}
if (!voisin) {
b.append(" -1 -1");
}
}
if (c.y + 1 >= height || height == 1) {
b.append(" -1 -1");
} else {
int i = 1;
boolean voisin = false;
while (c.y + i < height) {
final Case voisinDessous = grille[c.y + i][c.x];
if (voisinDessous.isNode) {
b.append(" " + voisinDessous.x + " " + voisinDessous.y);
voisin = true;
break;
}
i++;
}
if (!voisin) {
b.append(" -1 -1");
}
}
return b.toString();
}
return "";
}
@Override
public String solve(final Scanner in) {
width = in.nextInt();
height = in.nextInt();
final Case[][] grille = new Case[height][width];
if (in.hasNextLine()) {
in.nextLine();
}
final String[] lines = new String[height];
for (int i = 0; i < height; i++) {
final String line = in.nextLine();
lines[i] = line;
final Case[] ligne = new Case[width];
int j = 0;
for (final char c : line.toCharArray()) {
if (c == '0') {
ligne[j] = new Case(j, i, true);
} else {
ligne[j] = new Case(j, i, false);
}
j++;
}
grille[i] = ligne;
}
final StringBuilder builder = new StringBuilder();
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
final String compute = compute(grille[i][j], grille);
if (!compute.isEmpty()) {
builder.append(compute + "\n");
}
}
}
return builder.toString();
}
}