-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay38s1.java
More file actions
335 lines (278 loc) · 9.83 KB
/
Day38s1.java
File metadata and controls
335 lines (278 loc) · 9.83 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
import java.util.Scanner;
import java.util.ArrayList;
import java.util.List;
public class Day38s1 {
public static void main(String[] args) {
HashiSolver solver = new HashiSolver();
solver.solve();
}
}
class HashiSolver {
private static final int EMPTY = 0;
private static final int ISLAND = 1;
private static final int HORIZONTAL_BRIDGE = 2;
private static final int VERTICAL_BRIDGE = 3;
private static final int DOUBLE_HORIZONTAL_BRIDGE = 4;
private static final int DOUBLE_VERTICAL_BRIDGE = 5;
private int[][] board;
private List<Island> islands;
private int size;
private Scanner scanner;
public HashiSolver() {
this.scanner = new Scanner(System.in);
this.islands = new ArrayList<>();
}
public void solve() {
System.out.println("Hashi Çözücüye Hoş Geldiniz!");
System.out.println("Bulmaca boyutunu girin (örn: 5):");
size = scanner.nextInt();
initializeBoard();
getIslands();
System.out.println("\nGirilen Bulmaca:");
printBoard();
if (solveHashi(0)) {
System.out.println("\nBulmaca Çözüldü!");
printBoard();
} else {
System.out.println("\nBu bulmaca çözülemez!");
}
}
private void initializeBoard() {
board = new int[size][size];
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
board[i][j] = EMPTY;
}
}
}
private void getIslands() {
System.out.println("Ada sayısını girin:");
int islandCount = scanner.nextInt();
for (int i = 0; i < islandCount; i++) {
System.out.println("Ada " + (i + 1) + " için:");
System.out.println("Koordinatları girin (satır sütun):");
int row = scanner.nextInt() - 1;
int col = scanner.nextInt() - 1;
System.out.println("Bağlantı sayısını girin (1-8):");
int connections = scanner.nextInt();
islands.add(new Island(row, col, connections));
board[row][col] = ISLAND;
}
}
private boolean solveHashi(int islandIndex) {
if (islandIndex == islands.size()) {
return isSolved();
}
Island current = islands.get(islandIndex);
if (current.connections == 0) {
return solveHashi(islandIndex + 1);
}
// Yatay köprüleri dene
for (int i = 0; i < islands.size(); i++) {
Island other = islands.get(i);
if (canConnectHorizontally(current, other)) {
// Tek köprü
if (addHorizontalBridge(current, other)) {
if (solveHashi(islandIndex)) {
return true;
}
removeHorizontalBridge(current, other);
}
// Çift köprü
if (addDoubleHorizontalBridge(current, other)) {
if (solveHashi(islandIndex)) {
return true;
}
removeDoubleHorizontalBridge(current, other);
}
}
}
// Dikey köprüleri dene
for (int i = 0; i < islands.size(); i++) {
Island other = islands.get(i);
if (canConnectVertically(current, other)) {
// Tek köprü
if (addVerticalBridge(current, other)) {
if (solveHashi(islandIndex)) {
return true;
}
removeVerticalBridge(current, other);
}
// Çift köprü
if (addDoubleVerticalBridge(current, other)) {
if (solveHashi(islandIndex)) {
return true;
}
removeDoubleVerticalBridge(current, other);
}
}
}
return false;
}
private boolean canConnectHorizontally(Island a, Island b) {
if (a.row != b.row) return false;
if (a.col >= b.col) return false;
for (int col = a.col + 1; col < b.col; col++) {
if (board[a.row][col] != EMPTY) return false;
}
return true;
}
private boolean canConnectVertically(Island a, Island b) {
if (a.col != b.col) return false;
if (a.row >= b.row) return false;
for (int row = a.row + 1; row < b.row; row++) {
if (board[row][a.col] != EMPTY) return false;
}
return true;
}
private boolean addHorizontalBridge(Island a, Island b) {
if (a.connections < 1 || b.connections < 1) return false;
for (int col = a.col + 1; col < b.col; col++) {
board[a.row][col] = HORIZONTAL_BRIDGE;
}
a.connections--;
b.connections--;
return true;
}
private boolean addDoubleHorizontalBridge(Island a, Island b) {
if (a.connections < 2 || b.connections < 2) return false;
for (int col = a.col + 1; col < b.col; col++) {
board[a.row][col] = DOUBLE_HORIZONTAL_BRIDGE;
}
a.connections -= 2;
b.connections -= 2;
return true;
}
private boolean addVerticalBridge(Island a, Island b) {
if (a.connections < 1 || b.connections < 1) return false;
for (int row = a.row + 1; row < b.row; row++) {
board[row][a.col] = VERTICAL_BRIDGE;
}
a.connections--;
b.connections--;
return true;
}
private boolean addDoubleVerticalBridge(Island a, Island b) {
if (a.connections < 2 || b.connections < 2) return false;
for (int row = a.row + 1; row < b.row; row++) {
board[row][a.col] = DOUBLE_VERTICAL_BRIDGE;
}
a.connections -= 2;
b.connections -= 2;
return true;
}
private void removeHorizontalBridge(Island a, Island b) {
for (int col = a.col + 1; col < b.col; col++) {
board[a.row][col] = EMPTY;
}
a.connections++;
b.connections++;
}
private void removeDoubleHorizontalBridge(Island a, Island b) {
for (int col = a.col + 1; col < b.col; col++) {
board[a.row][col] = EMPTY;
}
a.connections += 2;
b.connections += 2;
}
private void removeVerticalBridge(Island a, Island b) {
for (int row = a.row + 1; row < b.row; row++) {
board[row][a.col] = EMPTY;
}
a.connections++;
b.connections++;
}
private void removeDoubleVerticalBridge(Island a, Island b) {
for (int row = a.row + 1; row < b.row; row++) {
board[row][a.col] = EMPTY;
}
a.connections += 2;
b.connections += 2;
}
private boolean isSolved() {
// Tüm adaların bağlantıları tamamlanmış mı kontrol et
for (Island island : islands) {
if (island.connections != 0) {
return false;
}
}
// Tüm adalar birbirine bağlı mı kontrol et
boolean[] visited = new boolean[islands.size()];
dfs(0, visited);
for (boolean v : visited) {
if (!v) return false;
}
return true;
}
private void dfs(int islandIndex, boolean[] visited) {
visited[islandIndex] = true;
Island current = islands.get(islandIndex);
for (int i = 0; i < islands.size(); i++) {
if (!visited[i]) {
Island other = islands.get(i);
if (isConnected(current, other)) {
dfs(i, visited);
}
}
}
}
private boolean isConnected(Island a, Island b) {
if (a.row == b.row) {
for (int col = Math.min(a.col, b.col) + 1; col < Math.max(a.col, b.col); col++) {
if (board[a.row][col] != HORIZONTAL_BRIDGE &&
board[a.row][col] != DOUBLE_HORIZONTAL_BRIDGE) {
return false;
}
}
return true;
}
if (a.col == b.col) {
for (int row = Math.min(a.row, b.row) + 1; row < Math.max(a.row, b.row); row++) {
if (board[row][a.col] != VERTICAL_BRIDGE &&
board[row][a.col] != DOUBLE_VERTICAL_BRIDGE) {
return false;
}
}
return true;
}
return false;
}
private void printBoard() {
System.out.println();
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
switch (board[i][j]) {
case EMPTY:
System.out.print(". ");
break;
case ISLAND:
System.out.print("O ");
break;
case HORIZONTAL_BRIDGE:
System.out.print("- ");
break;
case VERTICAL_BRIDGE:
System.out.print("| ");
break;
case DOUBLE_HORIZONTAL_BRIDGE:
System.out.print("=");
break;
case DOUBLE_VERTICAL_BRIDGE:
System.out.print("‖");
break;
}
}
System.out.println();
}
}
}
class Island {
int row;
int col;
int connections;
public Island(int row, int col, int connections) {
this.row = row;
this.col = col;
this.connections = connections;
}
}