Problems
Cody-Jamal's latest artistic installment is a tiled kitchen floor that can be retiled to different
patterns. The floor consists of a matrix of
To retile the kitchen, there are two allowed operations:
- flip a tile, changing its visible color from magenta to green, or vice versa, and
- swap two adjacent tiles (horizontally or vertically, but not diagonally), without flipping either.
Viewing Cody-Jamal's artistic floor is free, but interacting with it is not.
Performing a single flip operation costs
You can see the current state of the floor and want to turn it into a particular pattern. What is the minimum amount of coins you need to spend to achieve your goal?
Input
The first line of the input gives the number of test cases, M
if the currently visible side is magenta and G otherwise.
The last
Output
For each test case, output one line containing Case #,
where
Example #1
2
2 4 1 1
MGMG
MMMG
GMGM
MMMM
3 3 1 1
MGG
GMG
MMM
MMM
MGM
MMG
Case #1: 3
Case #2: 4
Example #2
1
1 5 1000 1
MGGGG
GGGMM
Case #1: 1003