(POJ-3279)Fliptile (dfs经典---也可以枚举)
Posted 进击的黑仔
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了(POJ-3279)Fliptile (dfs经典---也可以枚举)相关的知识,希望对你有一定的参考价值。
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
嗯,这个题我觉得很好啊,是dfs入门的经典题型啊,关键是翻转一次会影响周围的状态,除了用dfs以外,也可以用枚举(开关问题),
这里我只使用了dfs(枚举还不会orz)。
dfs搜索的话,肯定不是按一条一条路径来搜,我们可以dfs第一行的所有翻转情况,然后从第一行的情况可以递推出下面行的状态,
下面行中只要上行对应列有黑色就把这个位置翻转,因为会影响周围,所有上面行也会翻转。
至于字典序,因为一开始dfs第一行的时候是直接搜到底的,所以后面的dfs情况都是从最右边开始向左翻转的,如果有相同的最小值,那么记录下的一定是最右边的情况。
注意路径的回溯
1 #include<bits/stdc++.h> 2 3 using namespace std; 4 5 int m,n; 6 int ans; 7 8 int maps[16][16]; 9 int vis[16][16]; 10 int vistmp[16][16]; 11 int way[5][2] = {0,0,1,0,0,1,0,-1,-1,0}; 12 void inif() 13 { 14 memset(vis,0,sizeof(vis)); 15 memset(vistmp,0,sizeof(vistmp)); 16 for(int i=1;i<=m;i++) 17 { 18 for(int j=1;j<=n;j++) 19 { 20 scanf("%d",&maps[i][j]); 21 } 22 } 23 } 24 void flip(int x,int y) 25 { 26 vistmp[x][y] ^= 1; 27 for(int i=0;i<5;i++) 28 { 29 int xx = x+way[i][0]; 30 int yy = y+way[i][1]; 31 if(0<xx && xx<=m && yy>0 && yy <=n) 32 { 33 maps[xx][yy] ^= 1; 34 } 35 } 36 } 37 void solve(int k,int cnt) 38 { 39 if(cnt >= ans)return; 40 if(k == m) 41 { 42 int i; 43 for(i=1;i<=n;i++) 44 if(maps[m][i])break; 45 if(i <= n)return; 46 ans = cnt; 47 memcpy(vis,vistmp,sizeof(vistmp)); 48 return; 49 } 50 for(int i=1;i<=n;i++) 51 { 52 if(maps[k][i]) 53 { 54 flip(k+1,i); 55 cnt++; 56 } 57 } 58 solve(k+1,cnt); 59 for(int i=1;i<=n;i++) 60 { 61 if(vistmp[k+1][i])flip(k+1,i); 62 } 63 return; 64 } 65 void dfs(int k,int cnt) 66 { 67 if(cnt >= ans)return; 68 else if(k == n+1) 69 { 70 solve(1,cnt); 71 return; 72 } 73 else 74 { 75 dfs(k+1,cnt); 76 flip(1,k); 77 dfs(k+1,cnt+1); 78 flip(1,k); 79 } 80 } 81 int main() 82 { 83 while(~scanf("%d%d",&m,&n)) 84 { 85 ans = 0x3f3f3f3f; 86 inif(); 87 dfs(1,0); 88 if(ans == 0x3f3f3f3f)printf("IMPOSSIBLE\n"); 89 else 90 { 91 for(int i=1;i<=m;i++) 92 { 93 for(int j=1;j<=n;j++) 94 { 95 if(j != 1) 96 printf(" %d",vis[i][j]); 97 else 98 printf("%d",vis[i][j]); 99 } 100 puts(""); 101 } 102 } 103 } 104 }
以上是关于(POJ-3279)Fliptile (dfs经典---也可以枚举)的主要内容,如果未能解决你的问题,请参考以下文章