2116. Flip Game

单点时限: 2.0 sec

内存限制: 256 MB

Flip game is played on a rectangular nm field with two-sided pieces placed on each of its nm squares. One side of each piece is white and the other one is black and each piece is lying either it’s black or white side up. Each round you flip 3 to 5 pieces, thus changing the color of their upper side from black to white and vice versa. The pieces to be flipped are chosen every round according to the following rules:

  1. Choose any one of the pieces.

  2. Flip the chosen piece and also all adjacent pieces to the left, to the right, to the top, to the bottom,to the top left,to the top right,to the bottom left and to the bottom right of the chosen piece (if there are any).

Consider the following position as an example:



Here “.” denotes pieces lying their black side up and “*” denotes pieces lying their white side up. If we choose to flip the 3st piece from the 3rd row (this choice is shown at the picture), then the field will become:






The goal of the game is to flip all pieces white side up. You are to write a program that will search for the minimum number of rounds needed to achieve this goal.

输入格式

There are many test cases!In each case,the first line are two interger n and m(1<=n,m<=8),and then n lines,each line contain m characters to describle a n*m rectangular field.

输出格式

Each line output a minimum number of touches required to achieve this goal, or return -1 if it is impossible.

样例

Input
5 5
*****
*...*
*...*
*...*
*****
3 3
**.
**.
...
4 4
*...
**..
..**
...*
Output
1
2
10

4 人解决,6 人已尝试。

4 份提交通过,共有 11 份提交。

7.7 EMB 奖励。

创建: 15 年,12 月前.

修改: 6 年,8 月前.

最后提交: 9 月,4 周前.

来源: N/A

题目标签