beecrowd | 1275
# Light Up

**Timelimit: 8**

By Ricardo Anido Brazil

Light Up is a puzzle set in a rectangular board divided in smaller squares. Some squares in the board are “empty” (white squares the ﬁgure below), some squares are “barriers” (dark squares in the ﬁgure below). A barrier square may have an integer number i associated to it (0 ≤ i ≤ 4).

Figure: (a) Puzzle with 6 rows, 7 columns and 7 barriers; (b) a solution to the puzzle.

In this puzzle the goal is to “light up” all the empty squares by placing lamps in some of them (lamps are depicted as circles in the ﬁgure). Each lamp illuminates the square it is on, plus all squares in line with it, horizontally or vertically, up to a barrier square or the board end.

A winning conﬁguration satisﬁes the following conditions:

• all empty squares must be lit;

• no lamp may be lit by another lamp;

• all numbered barrier squares must have exactly that number of lamps adjacent to them (in the four squares above, below, and to the side);

• non-numbered barrier squares may have any number of lamps adjacent to them.

You must write a program to determine the smallest number of lamps that are needed to reach a winning conﬁguration.

The input contains several test cases. The ﬁrst line of a test case contains two integers **N**, **M** indicating respectively the number of rows and the number of columns of the board (1 ≤ **N** ≤ 7, 1 ≤ **M** ≤ 7). The second line contains one integer **B** indicating the number of barrier squares (0 ≤ **B** ≤ **N** × **M**). Each of the next **B** lines describe a barrier, containing three integers **R**, **C** and **K**, representing respectively the row number (1 ≤ **R** ≤ **N**), the column number (1 ≤ **C** ≤ **M**) and the barrier number (−1 ≤ **K** ≤ 4); **K** = −1 means the barrier is unnumbered. The end of input is indicated by **N** = **M** = 0.

For each test case in the input your program must produce one line of output, containing either an integer indicating the smallest number of lamps needed to reach a winning conﬁguration, in case such a conﬁguration exists, or the words ‘No solution’.

Sample Input | Sample Output |

2 2 |
2 |