Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programming Puzzle: How to paint a board?

There is a N x M board we should paint. We can paint either an entire row or an entire column at once. Given an N x M matrix of colours of all board cells find the minimal number of painting operations to paint the board.

For example: we should paint a 3 x 3 board as follows (R - red, B - blue, G - green):

B, B, B
B, R, R
B, G, G

The minimal number of painting operations is 4:

  • Paint row 0 with Blue
  • Paint row 1 with Red
  • Paint row 2 with Green
  • Paint column 0 with Blue

How would you solve it ?

like image 855
Michael Avatar asked Apr 28 '12 14:04

Michael


2 Answers

This looks like a fun problem. Let me take a shot at it with some pseudocode.

Function MinPaints(Matrix) Returns Integer
    If the matrix is empty return 0
    Find all rows and columns which have a single color
    If there are none, return infinity, since there is no solution
    Set the current minimum to infinity
    For each row or column with single color:
        Remove the row/column from the matrix
        Call MinPaints with the new matrix
        If the result is less than the current minimum, set the current minimum to the result
    End loop
    Return the current minimum + 1
End Function

I think that will solve your problem, but I didn't try any optimization or anything. This may not be fast enough though, I don't know. I doubt this problem is solvable in sub-exponential time.

Here is how this algorithm would solve the example:

BBB
BRR
BGG
|
+---BRR
|   BGG
|   |
|   +---RR
|   |   GG
|   |   |
|   |   +---GG
|   |   |   |
|   |   |   +---[]
|   |   |   |   |
|   |   |   |   Solvable in 0
|   |   |   |
|   |   |   Solvable in 1
|   |   |
|   |   +---RR
|   |   |   |
|   |   |   +---[]
|   |   |   |   |
|   |   |   |   Solvable in 0
|   |   |   |
|   |   |   Solvable in 1
|   |   |
|   |   Solvable in 2
|   |
|   Solvable in 3
|                       BB
+---Another branch with RR ...
|                       GG
Solvable in 4
like image 51
Kendall Frey Avatar answered Oct 12 '22 23:10

Kendall Frey


For starters, you can try an informed exhaustive search.

Let your states graph be: G=(V,E) where V = {all possible boards} and E = {(u,v) | you can move from board u to v within a single operation}.

  • Note that you do not need to generate the graph in advance - you can generate it on the fly, using a successors(board) function, that return all the successors of the given board.

You will also need h:V->R - an admissible heuristic function that evaluates the board1.

Now, you can run A*, or bi-directional BFS search [or combination of both], your source will be a white board, and your target is the requested board. Because we use admissible heuristic function - A* is both complete (always finds a solution if one exists) and optimal (finds the shortest solution), it will find the best solution. [same goes for bi-directional BFS].

drawbacks:

  • Though the algorithm is informed, it will have exponential behavior. But if it is an interview question, I believe a non-efficient solution is better then no solution.
  • Though complete and optimal - if there is no solution - the algorithm may be stuck in an infinite loop, or a very long loop at the very least until it realizes it has exuahsted all possibilities.

(1) example for admissible heuristic is h(board) = #(miscolored_squares)/max{m,n}

like image 30
amit Avatar answered Oct 12 '22 23:10

amit