'Implementing an algorithm to check a nonogram solution
I am trying to write an algorithm to check if a nonogram has been solved. I have already create multiple classes to have helper methods. The design for what I have so far is a class for Clues where the constructor looks like this:
public Clues(int[][] rowClues, int[][] colClues) { // constructor stuff }
I am storing the clues in these 2D arrays. For example, this puzzle would have the clues stored like this:
int[][] rowClues =
new int[][] {
new int[] {0, 2},
new int[] {1, 2},
new int[] {0, 3},
new int[] {0, 3},
new int[] {1, 1},
};
int[][] colClues =
new int[][] {
new int[] {1, 1},
new int[] {0, 1},
new int[] {0, 3},
new int[] {0, 3},
new int[] {3, 1},
};
I created another class called Puzzle that is composed of a board (for the game) and the clues. I am trying to write a function called isSolved to check if the Puzzle has been solved, but I am struggling on how to come up with an algorithm.
I currently have this:
public boolean isSolved() {
boolean flag = false;
for (int i=0; i<clue.getColCluesLength(); i++) {
flag = checkColumn(getBoard().getBoardColumn(i), getClues().getColClues(i));
if (flag == false) {
return false;
}
}
for (int i=0; i<clue.getRowCluesLength(); i++) {
flag = checkRow(getBoard().getBoardRow(i), getClues().getRowClues(i));
if (flag == false) {
return false;
}
}
return flag;
}
public boolean isRowSolved() {
for (int i=0; i< clue.getRowCluesLength(); i++) {
for (int j=0; j< clue.getRowClues(i).length; j++) {
}
}
return false;
}
The board (2D array) I have stores int's, and certain values represent elimination, space, and shaded.
I'm thinking I could compare the existing array in the board with the clues array, but I'm not sure how to exactly do that.
Some helper methods for this part are: int[] getRowClues(int index), int getColRowCluesLength(), int getWidth() (# of cells horizontally in each row of puzzle), getBoard().isShaded(), getBoard().isEliminated(), getBoard().isSpace()
Solution 1:[1]
public class Nonogram {
public static void main(String[] args) {
char[][] input = {
{'W', 'W'},
{'B', 'B'},
{'B', 'B'},
{'W', 'B'}
};
int[][] rowInstructions = {{}, {2}, {2}, {1}};
int[][] colInstructions = {{2}, {2}};
System.out.println(nonoChecker(input, rowInstructions, colInstructions));
}
public static boolean nonoChecker(char[][]input, int[][] rowInstructions,
int[][] colInstructions) {
for (int i = 0; i<rowInstructions.length; i++) {
List<Integer> rowEncoding = rowEncoding(input, i);
int[] encoded = rowEncoding.stream().mapToInt(c->c).toArray();
if (!Arrays.equals(encoded, rowInstructions[i])) {
return false;
}
}
for (int i = 0; i<colInstructions.length; i++) {
List<Integer> columnEncoding = columnEncoding(input, i);
int[] encoded = columnEncoding.stream().mapToInt(c->c).toArray();
if (!Arrays.equals(encoded, colInstructions[i])) {
return false;
}
}
return true;
}
public static List<Integer> rowEncoding(char[][] input, int row) {
List<Integer> result = new LinkedList<>();
int count = 0;
for (int j = 0; j < input[row].length; j++) {
if (input[row][j] == 'B') {
count++;
} else if (count > 0) {
result.add(count);
count = 0;
}
}
if (count > 0) {
result.add(count);
}
return result;
}
public static List<Integer> columnEncoding(char[][] input, int col) {
List<Integer> result = new LinkedList<>();
int count = 0;
for (int i = 0; i < input.length; i++) {
if (input[i][col] == 'B') {
count++;
} else if (count > 0) {
result.add(count);
count = 0;
}
}
if (count > 0) {
result.add(count);
}
return result;
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Coder |
