Peanut has recently been obsessed with playing Candy Crush. Candy Crush is a game played by matching candies of similar color together, in order to "pop" them and gain points. The points then add up and contribute to your end score at the end of the level.
Peanut is so obsessed, in fact, that he has created a variant of this game to be used as a programming problem. Yay! In Peanut's variant of Candy Crush, a preset board is given to you and you are supposed to select horizontal or vertical lines of similar color that are at least of length 3 to "pop". When you pop a similarly colored line, you gain (L^2) points where L is the length of the line. Once popped, the candies stay where they are and can be popped again. In other words, the candies do not move AT ALL from turn to turn and the board remains the same.
However, there is a twist. The board starts off with a kind of special blocker. Each of these blockers occupy an entire column and prevent you from popping ANY similarly colored line that involves a candy in one of these blocked off columns. These blockers can only be removed by "popping" a candy beside one of these blockers, where from there on, they will be removed permanently and will not regenerate.
The board will be of size N x N. At the start of the game, the blockers will occupy every column EXCEPT column C. There are three variants of candy in this game, orange candy, green candy and red candy. Each of these are designated by the letters "o", "g" and "r" on the input grid. Also, you are limited to only P pops. Given all these parameters, output the maximum number of points you can gain.
The first line of input will contain three integers, N, C and P.
The following N lines of input will contain N characters each, representing the board using the representation as stated above.
The output should contain one integer, the maximum number of points you can gain by playing optimally.
3 2 4 oro grg orr
36
4 2 6 gror orrg rrrr roog
82
Subtask | Score |
---|---|
1 | 3 |
2 | 5 |
3 | 5 |
4 | 6 |
5 | 7 |
6 | 9 |
7 | 10 |
8 | 13 |
9 | 20 |
10 | 22 |
11 | 0 |