Poj - 3279 Fliptile
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 10018 | Accepted: 3717 |
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
#include<cstdio> #include<iostream> #include<algorithm> #include<cstring> using namespace std; struct node { int map[20][20];//把地图写成结构体,便于直接复制 }; int n,m; int dir[5][2]={ 0,0,1,0,0,1,-1,0,0,-1 }; int a[20][20]; int b[20]; node mm;//mm作为临时地图 int ans[20][20]; int cnt=0,lastcnt=0;//cnt是翻转次数,lastcnt是上一次操作的翻转次数 void flip(int x,int y) { for(int i=0;i<5;i++) { int a=x+dir[i][0]; int b=y+dir[i][1]; if(mm.map[a][b])mm.map[a][b]=0; else mm.map[a][b]=1; } } int fun() { for(int i=2;i<=m;i++) { for(int j=1;j<=n;j++) { if(mm.map[i-1][j]) { a[i][j]=1; cnt++; flip(i,j); } } } for(int j=1;j<=n;j++) if(mm.map[m][j])return 0;//不能使地图最后一行全为0,返回0 return 1;//否则返回1 } int main() { ios::sync_with_stdio(false); cin.tie(0); node M; int flag=0; cin>>m>>n; for(int i=1;i<=m;i++) { for(int j=1;j<=n;j++) cin>>M.map[i][j]; } for(int i=0;i<=n;i++)//i表示第一行操作中有几个1 { memset(b,0,sizeof(b)); for(int j=n-i+1;j<=n;j++)b[j]=1;//b表示第一行的操作 do { mm=M; cnt=i;//cnt表示翻转次数 memset(a,0,sizeof(a)); for(int i=1;i<=n;i++) if(b[i])flip(1,i); if(fun()&&(cnt<=lastcnt||lastcnt==0))//先比较cnt大小 { flag++; if(cnt<lastcnt||lastcnt==0) { for(int i=1;i<=n;i++)ans[1][i]=b[i]; for(int i=2;i<=n;i++) { for(int j=1;j<=n;j++) ans[i][j]=a[i][j]; } } else if(cnt==lastcnt)//如果cnt等于lastcnt,看字典序大小 { bool f=false; for(int i=1;i<n;i++)//比较字典序 if(b[i]<ans[1][i]) { f=true; break; } else if(b[i]>ans[1][i])break; if(f) { for(int i=1;i<=n;i++)ans[1][i]=b[i]; for(int i=2;i<=n;i++) { for(int j=1;j<=n;j++) ans[i][j]=a[i][j]; } } } lastcnt=cnt; //只有能使地图全为0的cnt才能更新lastcnt的值 ,也就是说写到花阔号外是错的 } }while(next_permutation(b+1,b+n+1));//用next_permutation枚举出所有的可能 } if(!flag)cout<<"IMPOSSIBLE"<<endl; else { for(int i=1;i<=m;i++) { for(int j=1;j<n;j++) cout<<ans[i][j]<<" "; cout<<ans[i][n]<<endl; } } return 0; }