Okay, I have this task: John's bathroom floor was broken. We have a map of this floor, where '.' is good plate, and '+' is bad plate, for example:
.+++
.+.+
Here we have 5 broken plates, and 3 good ones. There are two kinds of plates, which are sold in shop: 1x1 plates and 2x1 plates. 1x1 plate costs A, and 2x1 plate costs B. Task is: given map of floor, count minimum price of floor fixing.
Looking at example on top: we can place 2 2x1 plates and 1 1x1 plate. So price will be A+2*B.
I have an idea: for every broken plate count maximum length of connected broken plates. Then price is length/2*B + length%2*A.
Problem is, that I really don't know how to do it. I have an idea of some recursive algorithm, but there are so many problems like circles:
+++
+.+
+++
So I have two questions:
- Am i going in the right direction?
- Can you give me any hints on implementing this algorithm?
Thank you!
EDIT
There is trivial case when 2*A < B, but let's talk about non-trivial=)
/EDIT