Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 13631 | Accepted: 5027 |
Description
Farmer John knows that an intellectually satisfied cow is a happy cow who will give more milk. He has arranged a brainy activity for cows in which they manipulate an M × N grid (1 ≤ M ≤ 15; 1 ≤ N ≤ 15) of square tiles, each of which is colored black on one side and white on the other side.
As one would guess, when a single white tile is flipped, it changes to black; when a single black tile is flipped, it changes to white. The cows are rewarded when they flip the tiles so that each tile has the white side face up. However, the cows have rather large hooves and when they try to flip a certain tile, they also flip all the adjacent tiles (tiles that share a full edge with the flipped tile). Since the flips are tiring, the cows want to minimize the number of flips they have to make.
Help the cows determine the minimum number of flips required, and the locations to flip to achieve that minimum. If there are multiple ways to achieve the task with the minimum amount of flips, return the one with the least lexicographical ordering in the output when considered as a string. If the task is impossible, print one line with the word "IMPOSSIBLE".
Input
Lines 2..M+1: Line i+1 describes the colors (left to right) of row i of the grid with N space-separated integers which are 1 for black and 0 for white
Output
Sample Input
4 4 1 0 0 1 0 1 1 0 0 1 1 0 1 0 0 1
Sample Output
0 0 0 0 1 0 0 1 1 0 0 1 0 0 0 0
Source
大意:一个m*n的01矩阵,每次点击(x,y),那么她的上下左右以及本身就会0变1,1变0,问把矩阵变成全0的,最小需要点击多少步。
想法:枚举第一行的所有可能,对于每种可能找出最优解。
需要干的活就是枚举第一行的所有情况然后对于每次枚举都计算验证是否符合要求以及反转所需的次数。其中,第一行的状态数量可以使用左移运算优化(效率比pow高),于是总共枚举的数量就有1<<col次。另外,因为这使得一行的状态是由一个数字保存的,所以依然使用位运算取得是否翻转的状态。
将枚举好的一次首行状态存好后就可以交给calc计算和验证了。验证就是通过上述翻转规则操作。其中get(x,y)为取得某个位置是否为1的状态的方法(过程)。
可以玩这个游戏找找规律 http://s1.4399.com:8080/4399swf/upload_swf/ftp/20080527/1.swf
详见代码注释
1 [20][20]; 2 int a[20][20]; 3 int q[20][20]; 4 int n, m; 5 6 bool get(int x, int y) 7 { 8 int i; 9 int s = a[x][y];//初始化为本身 10 for (i = 0; i < 5; i++) 11 { 12 int xx = x + d[i][0]; 13 int yy = y + d[i][1]; 14 if (xx <= 0 || y <= 0 || x > n || y > m) continue; 15 s +=p[xx][yy]; 16 } 17 return (s+1)%2; 18 /*或者if (s & 1)return 0; 19 return 1;*/ 20 } 21 22 int cal() 23 { 24 int i, j; 25 int res =0; 26 for (i = 2; i <= n; i++) 27 { 28 for (j = 1; j <= m; j++) 29 { 30 if (!get(i - 1, j))//通过翻转次数+原本的a[x][y]来判断要不要转 31 { 32 p[i][j] = 1;//用下一排来控制上一排,让其恢复 33 } 34 } 35 } 36 for (j = 1; j <= m; j++) 37 { 38 if (!get(n, j)) 39 return -1; 40 } 41 for (i = 1; i <= n; i++) 42 for (j = 1; j <= m; j++) 43 res += p[i][j];//计算转的总次数 44 return res; 45 } 46 47 int main() 48 { 49 cin >> n >> m; 50 int ans = inf; 51 int i, j; 52 for (i = 1; i <= n; i++) 53 for (j = 1; j <= m; j++) 54 cin >> a[i][j]; 55 for (i =0; i <(1<< m); i++)//二进制枚举,到2的m次方 56 { 57 memset(p,0,sizeof(p)); 58 for (j =1; j <=m; j++) 59 p[1][j] = i >>(j-1)& 1;//是右移,二进制枚举子集 60 int mm = cal(); 61 if (mm != -1 && ans > mm)//更新 62 { 63 ans = mm; 64 memcpy(q, p, sizeof(p));//拷贝函数 65 } 66 } 67 if (ans == inf) 68 cout << "IMPOSSIBLE" << endl; 69 else 70 { 71 for (i = 1; i <= n; i++) 72 { 73 for (j = 1; j <= m; j++) 74 { 75 cout << q[i][j]; 76 if (j == m) cout << " "; 77 else cout <<" "; 78 } 79 } 80 } 81 return 0; 82 }