This page is located in archive.

Task 2 - EN - Nonograms


Your task is to write an algorithm that solves a nonogram. The nonograms are brain teasers, defined by a legend linked to a grid used to draw a picture. Each number in the legend sets the number of the following boxes filled with color which belongs to the given number. The following rules hold:

  • there is always at least one empty box between two blocks of the boxes filled with the same color
  • there does not have to be an empty box between the boxes filled with different color
  • the order of the numbers in the legend corresponds to the order of the blocks of the boxes (from left to right, from top to bottom)

How to solve the task


The program is going to read from the standard input that has the following format:


where each constraint has the format:


where the field “color” applies to the following number and is in a format of the character that represents the color you will use for drawing (for example '#'). The field “number” sets the size of the given block.

Input examples: csp_example.txt, input.txt, krtkek.txt, dino.txt

Output examples: csp_example.txt.out input.txt.out krtek.txt.out, dino.out.txt


  • Use the standard output for the picture.
  • Draw the picture line-by-line, for the colored box write the sign of the given color, for the empty box write “_”.
  • In case multiple pictures satisfy the constraints, draw all of them separated with an empty line.
  • If the solution does not exist, output “null”.

Important Information

Think thoroughly through the way how you formalize the task - what is going to be a variable, how are the constraints and whether the edge consistency algorithms may be applied. Start with easy depth search and improve it by using CSP techniques.

In your main class write to the comment section what formalisation you have chosen, how you represent the constraints and what CSP algorithms are implemented.

In the automatic evaluation, your program has to solve 6 different problems in the total time limit of 15 minutes.


You can get up to 14 points for your task:

  • 4 points for correct implementation (automatically evaluated)
  • 4 points for correct CSP implementation
  • 3 points for correct AC3 implementation
  • 2 points for advanced heuristics (Forward checking, MCV, LCV)
  • 1 point for including a documentation describing how you formalized the CSP (variables, domains, constraints) and briefly summarizes of your implementation. It is enough to write it in the comments of the main class.

It is more important to have a correct CSP implementation and integration with AC-3 rather than the fastest algorithm for solving nonograms.

Implementation details

  • Create a package named “student”
  • Create the main class CSPMain in the “student” package, which is going to implement the main method.
  • Your algorithm has to print out all solutions
  • Submit the zipped package structure containing all your classes.


Week 5 - 08/04/2018 23:59

courses/b4b36zui/tasks/task2-malovane-krizovky-en.txt · Last modified: 2018/04/09 23:48 by schaemar