Dango Maker
Time limit2sMemory limit256 MB
Choose disjoint horizontal or vertical runs of three cells reading R, G, W in order on an N by M grid, maximizing how many such sticks fit.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Matrix, Implementation, Greedy
- Solved
- No attempts yet
Problem
You are a professional confectioner who makes dango, Japanese sweet dumplings. You are about to put the dumplings on skewers.
The dumplings sit on a grid of cells with rows and columns, one dumpling per cell. Each dumpling is red (R), green (G), or white (W).
To make one stick, you choose three consecutive cells and skewer their dumplings. The three cells must run from left to right or from top to bottom.
You want sticks whose dumplings are red, green, and white, in this order, and you want to make as many such sticks as possible. The order of the dumplings on a stick is the order in which they were chosen from the grid. A dumpling cannot be on more than one stick.
Given the colors of the dumplings on the grid, write a program that computes the maximum number of sticks you can make. The colors on every stick must be red, green, white, in this order.
Input
Read the following data from standard input.
- The first line contains two integers and separated by a space.
- The -th of the next lines () contains a string of length consisting of the characters R, G, and W. The -th character () of this string is the color of the dumpling in the -th row from the top and the -th column from the left.
Output
Write one line to standard output containing the maximum number of sticks you can make.