-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSkyscraper2.java
202 lines (139 loc) · 4.47 KB
/
Skyscraper2.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
import java.util.*;
import java.io.*;
public class Skyscraper2 {
public static HashSet<Integer>[][] generateCandidates(int n) {
HashSet<Integer>[][] candidates = new HashSet[n][n];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
{
candidates[i][j] = new HashSet<Integer>();
for (int x = 1; x <= n; x++)
candidates[i][j].add(x);
}
return candidates;
}
public static HashMap<String , HashSet<int[]>> generateClueMap(int n) {
HashMap<String , HashSet<int[]>> clueMap = new HashMap<>();
int[] nums = new int[n];
for (int i = 1; i <= nums.length; i++)
{
nums[i - 1] = i;
clueMap.put(String.valueOf(i) , new HashSet<int[]>());
}
permute(nums , 0 , clueMap);
return clueMap;
}
public static void permute(int[] nums , int index , HashMap<String , HashSet<int[]>> clueMap) {
if (index == nums.length)
clueMap.get(visibleSkyscrapers(nums)).add(Arrays.copyOf(nums , nums.length));
for (int i = index; i < nums.length; i++)
{
int temp = nums[i];
nums[i] = nums[index];
nums[index] = temp;
permute(nums , index + 1 , clueMap);
temp = nums[i];
nums[i] = nums[index];
nums[index] = temp;
}
}
public static String visibleSkyscrapers(int[] nums) {
int biggest = 0;
int count = 0;
for (int x : nums)
if (x > biggest)
{
count++;
biggest = x;
}
return String.valueOf(count);
}
public static void removeCandidates(HashSet<Integer>[][] candidates , HashMap<String , HashSet<int[]>> clueMap , String[] info) {
for (int side = 0; side < 4; side++)
for (int i = 0; i < candidates.length; i++)
removeSectionCandidates(getRowColCandidates(candidates , side , i) , clueMap.get(info[side].substring(i , i + 1)));
}
public static HashSet<Integer>[] getRowColCandidates(HashSet<Integer>[][] candidates , int side , int i) {
int n = candidates.length;
HashSet<Integer>[] section = new HashSet[n];
if (side == 1 || side == 2)
for (int j = 0; j < n; j++)
section[j] = candidates[i][side == 1 ? j : n - 1 - j];
else
for (int j = 0; j < n; j++)
section[j] = candidates[side == 0 ? j : n - 1 - j][i];
return section;
}
public static void removeSectionCandidates(HashSet<Integer>[] section , HashSet<int[]> clue) {
int n = section.length;
HashSet<Integer>[] allFits = new HashSet[n];
for (int i = 0; i < allFits.length; i++)
allFits[i] = new HashSet<Integer>();
for (int[] permutation : clue)
if (fit(section , permutation))
add(allFits , permutation);
for (int i = 0; i < section.length; i++)
section[i].retainAll(allFits[i]);
}
public static boolean fit(HashSet<Integer>[] section , int[] permutation) {
for (int i = 0; i < section.length; i++)
if (!section[i].contains(permutation[i]))
return false;
return true;
}
public static void add(HashSet<Integer>[] fits , int[] permutation) {
for (int i = 0; i < fits.length; i++)
fits[i].add(permutation[i]);
}
public static String[] info(String line , int n) {
String[] info = new String[4];
Arrays.fill(info , "");
StringTokenizer token = new StringTokenizer(line , " ");
info[0] = token.nextToken();
for (int i = 0; i < n; i++)
{
String row = token.nextToken();
info[1] += row.substring(0 , 1);
info[2] += row.substring(1 , 2);
}
info[3] = token.nextToken();
return info;
}
public static boolean done(HashSet<Integer>[][] board) {
for (int i = 0; i < board.length; i++)
for (int j = 0; j < board.length; j++)
if (board[i][j].size() > 1)
return false;
return true;
}
public static void print(HashSet<Integer>[][] nums) {
for (int i = 0; i < nums.length; i++)
System.out.print(Arrays.toString(nums[i]).replaceAll("[^0-9]", "") + (i != nums.length - 1 ? ", " : ""));
System.out.println("\n\n------------------------------\n");
}
public static void debug(HashSet<Integer>[][] nums) {
for (int i = 0; i < nums.length; i++)
System.out.println(Arrays.toString(nums[i]));
System.out.println();
}
public static void main(String[] args) throws IOException , InterruptedException {
File inFile = new File("SKYSCRAPER.IN");
Scanner scan = new Scanner(inFile);
for (int lines = 1; lines <= 5; lines++)
{
System.out.println("Line #" + lines + "\n");
int n = lines <= 3 ? 4 : 5;
HashMap<String , HashSet<int[]>> clueMap = generateClueMap(n);
HashSet<Integer>[][] candidates = generateCandidates(n);
String[] info = info(scan.nextLine() , n);
debug(candidates);
while (!done(candidates))
{
removeCandidates(candidates , clueMap , info);
debug(candidates);
}
print(candidates);
}
scan.close();
}
}