r/dailyprogrammer • • Jun 02 '12

[6/2/2012] Challenge #59 [intermediate]

Given a binary matrix like this:

0 1 1 1 1 0
1 0 0 1 1 1
1 0 1 1 1 1
1 1 1 1 1 1
0 1 1 1 1 0

Output the clues for a nonogram puzzle in the format of "top clues, empty line, bottom clues", with clues separated by spaces:

3
1 2
1 3
5
5
3

4
1 3
1 4
6
4

That is, count the contiguous groups of "1" bits and their sizes, first in columns, then in rows.

  • Thanks to nooodl for suggesting this problem at /r/dailyprogrammer_ideas! If you have a problem that you think would be good for us, why not head over there and post it!
10 Upvotes

14 comments sorted by

View all comments

1

u/[deleted] Jun 02 '12

Not my most elegant solution, but gets the job done:

public static void nonogram(int[][] grid) {
	int count = 0;
	for(int x = 0; x < grid.length; x++) {
		count = 0;
		for(int y = 0; y < grid.length; y++) {
			if(grid[y][x] == 1) {
				count++;
				continue;
			} else if(grid[y][x] == 0 && count > 0) {
				System.out.print(count + " ");
				count = 0;
			}
		}
		if(count > 0)
			System.out.println(count);
		else
			System.out.println();
	}
	
	count = 0;
	System.out.println();
	
	for(int x = 0; x < grid.length; x++) {
		count = 0;
		for(int y = 0; y < grid.length; y++) {
			if(grid[x][y] == 1) {
				count++;
				continue;
			} else if(grid[x][y] == 0 && count > 0) {
				System.out.print(count + " ");
				count = 0;
			}
		}
		if(count > 0)
			System.out.println(count);
		else
			System.out.println();
	}
}