POJ3279-Fliptile

来源:互联网 发布:中国亚投行失败了知乎 编辑:程序博客网 时间:2024/06/05 02:18
Fliptile
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 10375 Accepted: 3841

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 41 0 0 10 1 1 00 1 1 01 0 0 1

Sample Output

0 0 0 01 0 0 11 0 0 10 0 0 0

Source

/*有一个M×N 的格子,每个格子可以翻转正反面,它们一面是黑色,另一面是白色。黑色的格子翻转后就是白色,白色的格子翻转过来则是黑色。游戏要做的就是把所有的格子都翻转成白色。每次翻转一个格子时,与它上下左右相邻接的格子也会被翻转。因为翻格子太麻烦了,所以想通过尽可能少的次数把所有格子都翻成白色。现在给定了每个格子的颜色,请求出用最小步数完成时每个格子翻转的次数。最小步数的解有多个时,输出字典序最小的一组。解不存在的话,则输出IMPOSSIBLE。 范围M,N∈[1,15]0代表白色 1代表黑色EG:输入:M=4 N=41001011001101001输出:0000100110010000最少4次首先,同一个格子翻转两次的话就会恢复原状,所以多次翻转是多余的。联系POJ3276翻转的格子的集合相同的话,其次序是无关紧要的。因此,总共有2^(N*M)种翻转的方法。  最左端的格子反转的方法只有1种  这回若考虑最左上角的格子  在这里,除了翻转(1,1)之外,翻转(1,2)和(2,1)也可以把这个格子翻转,  所以像POJ3276题目那样直接确定的办法行不通。不妨先指定好最上面一行的翻转方法。  此时能够翻转(1,1)的只剩下(2,1)了,所以可以直接判断(2,1)是否需要翻转。  类似地(2,1)~(2,N)都能这样判断,如此反复下去就可以确定所有格子的翻转方法。  最后(M,1)~(M,N)如果并非全为白色,  就意味着不存在可行的操作方法。像这样,先确定第一行的翻转方式 有2^N种反转方式,  然后可以很容易判断这样是否存在解以及解的最小步数是多  少,这样将第一行的所有翻转方式都尝试一次就能求出整个问题的最小步数。  复杂度O(M*N*2^N)。  代码:*/# include <stdio.h># define MAX 16int FINA[MAX][MAX],DP[MAX][MAX],M,N;int S[MAX][MAX];int GET(int T[][MAX],int X,int Y);//计算(X,Y)的颜色int JS();//计算第一行确定的情况下的最小操作次数int main(){int Flag=-1,i,j,k,m,Q; //freopen("SWE.txt","r",stdin);scanf("%d %d",&M,&N);Q=1<<N; //Q为2^N次幂for(i=0;i<M;i++)//输入for(j=0;j<N;j++)//对S进行加工    scanf("%d ",&S[i][j]);    for(i=0;i<Q;i++)//确定第一行的方案{for(j=0;j<M;j++){   for(k=0;k<N;k++)     DP[j][k]=0;//每次枚举初始化}   for(j=0;j<N;j++)   DP[0][N-j-1]=i>>j&1;//枚举i从0---Q枚举 将i以二进制存入DP[0][j]  m=JS();//计算出第一行确定的情况下的最小次数if(m>=0&&(Flag>m||Flag<0)){Flag=m;//Flag存储最小次数           for(j=0;j<M;j++)   for(k=0;k<N;k++)      FINA[j][k]=DP[j][k];//将此次结果记录}}if(Flag<0)printf("IMPOSSIBLE\n");else{//printf("\n最少%d次\n",Flag);for(i=0;i<M;i++)//输出的是次数 不是反转完之后的矩阵{for(j=0;j<N;j++)printf("%d ",FINA[i][j]);printf("\n");}}return 0;}int GET(int T[][MAX],int X,int Y){    int C=T[X][Y],i,xx,yy;const char x[6]={-1,0,0,0,1};//对上下左右的格子的坐标    const char y[6]={0,-1,0,1,0};    for(i=0;i<5;i++){xx=X+x[i];yy=Y+y[i];if(xx>=0&&yy>=0&&xx<M&&yy<N)C+=DP[xx][yy];//如果在范围内DP[][]里面存的是上一行和左边的格子的反转情况  并更新C}//如果上面反转了5次 左边反转了4次 那么这个格子就是原来的T[X][Y]+4+5  偶数则为0(白) 奇数则为1(黑)    return C%2;// C&1 或C%2 运算用来判断是否奇数 奇数返回1 偶数返回0}int JS()//计算第一行确定的情况下的最小操作次数{int i,j,sum=0;for(i=0;i<M-1;i++){for(j=0;j<N;j++)if(GET(S,i,j))//如果(i,j)为黑色DP[i+1][j]=1;//反转下方格子}for(j=0;j<N;j++){if(GET(S,M-1,j))//如果最后一行不满足情况return -1;}for(i=0;i<M;i++)//如果满足 记录总步数{for(j=0;j<N;j++)   sum+=DP[i][j];}return sum; //返回总步数}


原创粉丝点击