poj 3279

Fliptile
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 3629   Accepted: 1400

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

Line 1: Two space-separated integers: M and N 
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

Lines 1..M: Each line contains N space-separated integers, each specifying how many times to flip that particular location.

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

题意:给你一个0,1组成的数组,按规则翻转,使其成为全0的数组,翻过的记为1,没翻的为0,输出是否翻转的数组
  翻转规则:自己以及上下左右共五个点,0变1,1变0
分析:第一行确定了的话,下面是否翻转就确定了,因为第一行不再翻转之后,如果有1,要变成0,只能翻转它对应位置的下一个翻转;
枚举第一行翻转的所有情况,找到翻转次数最小的。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int inf=0x3f3f3f3f;
int a[20][20],temp[20][20],tt[20][20],ans[20][20];
//a最开始输入的数据,temp每次翻转后临时记录数据的数组,tt,记录是否翻转的数组,ans,记录最终是否翻转的数组
int n,m,maxn; int dx[]={-1,0,0,0,1}; int dy[]={0,1,0,-1,0}; void get(int x,int y){//翻转 for(int i=0;i<5;i++){ int a=x+dx[i],b=y+dy[i]; if(a>=0&&a<m&&b>=0&&b<n) temp[a][b]^=1; } } int main(){ int i,j,k; while(~scanf("%d%d",&m,&n)){ for(i=0;i<m;i++) for(j=0;j<n;j++) scanf("%d",&a[i][j]); maxn=inf;//最开始翻转次数初始化最大值 memset(ans,0,sizeof(ans)); for(i=0;i<1<<n;i++){ memset(tt,0,sizeof(tt)); memcpy(temp,a,sizeof(a));//将a复制到temp int sum=0;//记录该状态要翻转的次数 for(j=0;j<n;j++){ tt[0][j]=i>>j&1; if(tt[0][j]==1){//上一个为0,则翻转 get(0,j); sum++;//每次翻转加1 } } for(k=1;k<m;k++) for(j=0;j<n;j++) if(temp[k-1][j]==1){//上一个为0,则翻转 tt[k][j]=1;//翻转要记录下来 get(k,j);sum++;//每次翻转加1 } for(j=0;j<n;j++) if(temp[m-1][j]==1){ sum=inf;break;//最后一行还有1,说明无法完成翻转,翻转次数改为最大值 } if(sum<maxn){//要按字典序排序,所以sum必须要小于maxn,不能等 maxn=sum; memcpy(ans,tt,sizeof(tt));//将答案记录到ans中 } }
//输出
if(maxn==inf){ printf("IMPOSSIBLE\n"); continue; } for(i=0;i<m;i++) for(j=0;j<n;j++){ printf("%d",ans[i][j]); if(j!=n-1) printf(" "); else printf("\n"); } } return 0; }

 

posted @ 2015-07-31 22:14  dreamOwn  阅读(206)  评论(0编辑  收藏  举报