Region Coloring
InterviewTime limit1sMemory limit512 MB
Given a grid of 0, 1, and 2 cells, find the fewest horizontal strokes that reproduce the picture, where strokes may overpaint but never cover a 0 cell.
Problem
Sanjini tries to copy a given picture onto an empty grid sheet, following the grid lines. The picture uses two colors.
The brush can paint only in the horizontal direction, and its width is 1 cell. A single stroke can be any length, and painting over an area that is already painted is allowed.
Find the minimum number of brush strokes Sanjini needs to copy the picture exactly.
Input
The first line contains two integers and , separated by a space, the number of rows and the number of columns of the picture.
Each of the next lines contains integers separated by spaces. Each integer describes one cell. '0' means the cell is not painted, and '1' and '2' mean the cell is painted with that color.
Output
Print the minimum number of brush strokes needed to copy the picture exactly.