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
|
import java.util.*;
public class SkyScrapers {
public static final int SIZE = 4;
public static final int[][] direction = new int[][]{{0,1}, {1,0}, {0,-1}, {-1, 0}};
public static int[][][] square3d;
public static int row = 0, col = 0;
public static int count;
static int[][] solvePuzzle (int[] clues) {
generateEmptySquare();
while(count < SIZE * SIZE) {
for(int i = 0; i < SIZE * 4; i++) {
System.out.printf("i = %d, clue[i]=%d, r = %d, c = %d%n", i, clues[i], row, col);
cleanAllHintsInLine(i);
checkLineForOneHint(i);
useClue(i, clues[i]);
if((i + 1) % SIZE != 0) {
row += direction[i / SIZE][0];
col += direction[i / SIZE][1];
}
}
checkSquareForOneHint();
}
int[][] result = new int[SIZE][SIZE];
for(int i = 0; i < SIZE; i++) {
for(int j = 0; j < SIZE; j++) {
result[i][j] = square3d[i][j][0];
}
}
return result;
}
static void useClue(int i, int clue) {
switch(clue) {
case 1:
setSkyscraper(iRow(i, 0), iCol(i, 0), SIZE);
cleanHintLine(i, SIZE);
break;
case 2:
cleanHint(i, 0, SIZE);
if(getSkyscraper(i, SIZE - 1) == SIZE) {
setSkyscraper(iRow(i, 0), iCol(i, 0), SIZE - 1);
}
if(getSkyscraper(i, SIZE - 2) == SIZE) {
cleanHint(i, 1, SIZE - 1);
}
if(getSkyscraper(i, SIZE - 2) == SIZE && getSkyscraper(i, SIZE - 1) == SIZE - 1) {
setSkyscraper(iRow(i, 0), iCol(i, 0), 2);
setSkyscraper(iRow(i, 1), iCol(i, 1), 1);
}
break;
case 3:
cleanHint(i, 0, SIZE);
cleanHint(i, 1, SIZE);
cleanHint(i, 0, SIZE - 1);
if(getSkyscraper(i, SIZE - 1) == SIZE && getSkyscraper(i, SIZE - 2) == SIZE - 1) {
setSkyscraper(iRow(i, 0), iCol(i, 0), 2);
setSkyscraper(iRow(i, 1), iCol(i, 1), 1);
} else if (getSkyscraper(i, SIZE - 1) == SIZE - 1 && getSkyscraper(i, SIZE - 2) == SIZE) {
setSkyscraper(iRow(i, 0), iCol(i, 0), 1);
setSkyscraper(iRow(i, 1), iCol(i, 1), 2);
} else if (getSkyscraper(i, SIZE - 2) == SIZE && getSkyscraper(i, 0) == 2) {
setSkyscraper(iRow(i, 1), iCol(i, 1), 3);
}
break;
case SIZE:
for(int j = 0; j < SIZE; j++) {
setSkyscraper(iRow(i, j), iCol(i, j), j + 1);
}
break;
}
}
static void generateEmptySquare() {
square3d = new int[SIZE][SIZE][SIZE+1];
for(int r = 0; r < SIZE; r++) {
for(int c = 0; c < SIZE; c++) {
for(int d = 0; d < SIZE + 1; d++) {
square3d[r][c][d] = d;
}
}
}
count = 0;
}
static void checkSquareForOneHint() {
for(int i = 0; i < SIZE; i++) {
for(int j = 0; j < SIZE; j++) {
checkIsOnlyOneHintAndSetIfYes(i, j);
}
}
}
static void checkLineForOneHint(int i) {
for(int h = 1; h <= SIZE; h++) {
int pos = 0;
int count = 0;
for(int j = 0; j < SIZE; j++) {
if(square3d[iRow(i, j)][iCol(i, j)][h] > 0) {
count++;
pos = j;
}
}
if(count == 1) {
setSkyscraper(iRow(i, pos), iCol(i, pos), h);
}
}
}
static boolean checkIsOnlyOneHintAndSetIfYes(int row, int col) {
if(square3d[row][col][0] > 0) {
return true;
}
int num = 0;
int count = 0;
for(int dig = 1; dig <= SIZE; dig++) {
if(square3d[row][col][dig] > 0) {
count++;
num = dig;
}
}
if(count == 1) {
setSkyscraper(row, col, num);
return true;
}
return false;
}
static void setSkyscraper(int row, int col, int value) {
if(square3d[row][col][0] == 0) {
square3d[row][col][0] = value;
for(int h = 0; h < SIZE; h++) {
square3d[row][col][h+1] = 0;
square3d[row][h][value] = 0;
square3d[h][col][value] = 0;
}
count++;
}
}
static int getSkyscraper(int i, int j) {
return square3d[iRow(i, j)][iCol(i, j)][0];
}
static int iRow(int i, int j) {
return row + direction[((i / SIZE) + 1) % SIZE][0] * j;
}
static int iCol(int i, int j) {
return col + direction[((i / SIZE) + 1) % SIZE][1] * j;
}
static int howManySee(int i) {
int max = 0;
int count = 0;
for(int j = 0; j < SIZE; j++) {
if(square3d[iRow(i, j)][iCol(i, j)][0] > max) {
max = square3d[iRow(i, j)][iCol(i, j)][0];
count++;
}
}
return count;
}
static void cleanAllHintsInLine(int i) {
for(int j=0; j < SIZE; j++) {
int v = getSkyscraper(i, j);
if(v > 0) {
cleanHintLine(i, v);
}
}
}
static void cleanHintLine(int i, int hint) {
for(int j = 0; j < SIZE; j++) {
cleanHint(i, j, hint);
}
}
static void cleanHint(int i, int j, int hint) {
square3d[iRow(i, j)][iCol(i, j)][hint] = 0;
}
}
|