-
Notifications
You must be signed in to change notification settings - Fork 0
/
Nonogram.java
294 lines (247 loc) · 6.25 KB
/
Nonogram.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
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
//package Lab02.Nonogram;
//@Troy Sorongon
//@03/10/2022
//CS245 Lab02: Nonogram
import java.util.Arrays;
public class Nonogram {
/**
* Method checks if the inputs for the row and columns satisfy the constraints for the Nonogram puzzle
* Constraints --> 1. 9x9 2. each row or column block set has at most 2 elements
* @param int[][] theRows, the row block sets
* @param int[][] theColumns, the column block sets
* @throws IllegalArgumentException if puzzle does not satisfy the constraints
*/
public static void validBoard(int[][]theRows, int[][]theColumns) throws IllegalArgumentException
{
// checks if either the length of rows or columns are greater than 9
if(theRows.length > 9 || theColumns.length > 9)
{
throw new IllegalArgumentException("Puzzle exceeds 9x9 matrix");
}
// checks if row block set has more than 2 elements
for(int i = 0; i < theRows.length; i++)
{
if(theRows[i].length > 2)
{
throw new IllegalArgumentException("Row exceeds more than 2 blocks");
}
}
// checks if column block set has more than 2 elements
for(int j = 0; j < theColumns.length; j++)
{
if(theColumns[j].length > 2)
{
throw new IllegalArgumentException("Column exceeds more than 2 blocks");
}
}
}
/**
*
* @param board boolean[][], puzzle is solved of length of rows and columns
* @param col int[][], the blocksets of the column
* @param rowLength int
* @param rowStart int, starting row position of blocks to be placed
* @param isComplete boolean, true if at the end of the row or else false
* @return true if board is safe to place blocks or else returns false
*/
public static boolean isSafe(boolean[][] board, int[][] col, int rowLength, int rowStart, boolean isComplete)
{
for(int c = 0; c < col.length; c++)
{
int blockset1 = col[c][0];
int blockset2 = col[c][1];
if(rowStart < blockset1 || rowStart < blockset2)
{
continue;
}
int placedBlocks = 0; // tracks the amount of consecutive blocks placed
int row = 0; // row index for board
// checks for 1st set of blocks
if(blockset1 != 0)
{
for(; row < rowLength; row++)
{
if(board[row][c] == true)
{
placedBlocks++;
}
else // if board[row][c] == false
{
placedBlocks = 0;
}
if(placedBlocks == blockset1)
{
row++;
if(board[row][c] == true) // if next position on the board is true, it is filled and not a valid move
{
return false;
}
else
{
break;
}
}
}
placedBlocks = 0; // reset counter to check blockset2
}
// checks for 2nd set of blocks
for(; row < rowLength; row++)
{
if(board[row][c] == true)
{
placedBlocks++;
}
else // if board[row][c] == false
{
placedBlocks = 0;
}
if(placedBlocks == blockset2)
{
row++;
break;
}
}
// checks if the remainder of the column is filled
for(; row < rowLength; row++)
{
if(board[row][c] == true)
{
return false;
}
}
// checks if current block is at the end of the row && placedBlocks haven't yet matched blockset2
if(isComplete && placedBlocks != blockset2)
{
return false;
}
}
return true;
}
private static void placeBlocks(int startRow, int startCol, int numBlocks, boolean[][] board)
{
for(int c = 0; c < numBlocks; c++)
{
board[startRow][startCol + c] = true;
}
}
private static void removeBlocks(int startRow, int startCol, int numBlocks, boolean[][] board)
{
for(int c = 0; c < numBlocks; c++)
{
board[startRow][startCol + c] = false;
}
}
public static boolean solve(boolean[][] board, int[][] rows, int[][] cols, int rowStart, int colStart, boolean idxZero)
{
// if the board is not safe to place blocks
if(!isSafe(board, cols, rows.length, rowStart, false))
{
return false;
}
// if at the end of the row
else if(rowStart == rows.length)
{
return isSafe(board, cols, rows.length, rowStart, true); // checks if blocks are placed correctly
}
else
{
int num;
if(idxZero == true && rows[rowStart][0] != 0)
{
num = rows[rowStart][0] + rows[rowStart][1];
}
else
{
num = colStart + rows[rowStart][1] - 1;
}
int cond = cols.length - num;
for(int i = 0; i < cond; i++)
{
boolean placed;
int idx;
if(idxZero)
{
idx = 0;
}
else
{
idx = 1;
}
int numBlocks = rows[rowStart][idx];
placeBlocks(rowStart, colStart + i, numBlocks, board);
if(idxZero == true)
{
int colIdx = 0;
if(rows[rowStart][0] != 0)
{
colIdx = rows[rowStart][0] + 1 + i;
}
placed = solve(board, rows, cols, rowStart, colStart + colIdx, false);
}
else
{
placed = solve(board, rows, cols, rowStart + 1, 0, true);
}
if(placed)
{
return true; // solved the puzzle
}
idx = 1;
if(idxZero)
{
idx = 0;
}
numBlocks = rows[rowStart][idx];
removeBlocks(rowStart, colStart + i, numBlocks, board);
}
}
return false; // was not able to be solved
}
public static boolean[][] solveBoard(int[][] rows, int[][] cols)
{
validBoard(rows, cols);
boolean[][] board = new boolean[rows.length][cols.length];
solve(board, rows, cols, 0, 0, true);
return board;
}
public static void printBoard(int[][] rows, int[][] cols, boolean[][] board)
{
System.out.print(" ");
for(int[] col : cols)
{
System.out.print(col[0] + " ");
}
System.out.println();
System.out.print(" ");
for(int[] col : cols)
{
System.out.print(col[1] + " ");
}
System.out.println();
char block;
for(int r = 0; r < rows.length; r++)
{
System.out.print(rows[r][0] + " " + rows[r][1] + " ");
for(int c = 0; c < board[0].length; c++)
{
if(board[r][c] == true)
{
block = 'O';
}
else
{
block = 'X';
}
System.out.print(block + " ");
}
System.out.println();
}
}
// Testing
public static void main(String[] args){
int[][] rows = {{4,3}, {3,4}};
int[][] columns = {{0,2}, {0,2}, {0,2}, {0,1}, {0,1}, {0,1}, {0,2}, {0,2}, {0,1}};;
boolean[][] board = solveBoard(rows, columns);
printBoard(rows, columns, board);
}
}