POJ3279 Fliptile【状态压缩+DFS】

Fliptile

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 17511   Accepted: 6409

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

Source

USACO 2007 Open Silver

问题链接:POJ3279 Fliptile

问题描述:给定M * N 个格子,每个格子可以翻转正反面,一面是黑色,另一面是白色。黑色翻转之后变成白色,白色翻转之后则变成黑色。现在要将所有的格子翻转为白色。不过每次翻转一个格子,与它上下左右相邻接的格子也会被翻转。求用最少的步骤完成任务时,每个格子的翻转次数。当有多个解时,输出字典序最小的一组;解不存在的话,则输出IMPOSSIBLE。

解题思路:一个格子反转偶数次和反转0次一样,反转奇数次和反转1次一样,因此最后的答案是一个01数组,即要么反转一次,要么不反转。如果穷举所有的结果会超时,现在考虑当第一行的状态固定时,格子(i,j)的状态只受格子(i+1,j)控制了,当格子(i,j)不为白色时,格子(i+1,j)就必须反转,当第二行的状态确定后,继续确定第三行,直到倒数第二行决定后,至此1-(m-1)行全为白色了,现在查看最后一行,如果最后一行都为白色,则是一种可行解。第一行共有2^n中状态,遍历各种状态求出最优解即可。

AC的C++程序:

#include<iostream>
#include<cstring>

using namespace std;
const int N=20;
int m,n;//行列

int g[N][N];//保存初始情况
int ans[N][N];//保存最优解
int flip[N][N];//保存中间解

//查询(x,y)位置的颜色 奇数为1,偶数为0 ,颜色有初始颜色g[x][y]和上下左右和自己是否反转共同决定 
//程序下标从1开始存储,因此可以不用进行下标越界查询 
int get(int x,int y)
{
	return (g[x][y]+flip[x][y]+flip[x-1][y]+flip[x+1][y]+flip[x][y-1]+flip[x][y+1])%2;
} 

//求出在第一行确定的情况下最少的操作次数,如果无解返回-1
int calc()
{
	//求出从第2行开始的反转方法
	int count=0;
	for(int i=2;i<=m;i++)
	  for(int j=1;j<=n;j++)
	    if(get(i-1,j))//如果(i-1,j)是黑色的话,则这个格子必须反转 
		  flip[i][j]=1;
	//判断最后一行是否为全为0
	for(int j=1;j<=n;j++) 
	  if(get(m,j))
	    return -1;
	//如果是可行解,返回反转的次数 
	for(int i=1;i<=m;i++)
	  for(int j=1;j<=n;j++)
	    count+=flip[i][j];
	return count; 
}

void solve()
{
	int res=-1;
	/*按照字典序尝试第一行的所有可能情况,使用状态压缩
	  第一行共n个格子,每个格子都有反转和不反转两种选择,因此共2^n种选择
	  状态压缩:1表示反转,0表示不反转
	  举例说明:当n=2时有两个格子,共4中选泽
	  00 01 10 11(低位表示序号小的格子)分别表示都不反转,反转第一个,反转第二个,两个都反转
	  四种状态的十进制数分别为0 1 2 3 。1<<(j-1)表示此状态下第j个格子是否被反转,即第j为是否为1	
	  那么((1<<(j-1))&i)==1表示第j个格子被反转了因此flip[1][j]=1 */ 
	for(int i=0;i<(1<<n);i++){
		memset(flip,0,sizeof(flip));
		for(int j=1;j<=n;j++) 
		  if((1<<(j-1))&i)
		    flip[1][j]=1;
		int count=calc();//统计这种情况下需要反转的次数
		if(count>=0&&(res<0||res>count)){//更新最优结果 
			res=count;
			memcpy(ans,flip,sizeof(flip));
		} 
	}
	if(res<0)//无解 
	  printf("IMPOSSIBLE\n"); 
	else{//输出结果 
		for(int i=1;i<=m;i++)
		  for(int j=1;j<=n;j++)
		    printf("%d%c",ans[i][j],j==n?'\n':' ');
	}
} 

int main()
{
	scanf("%d%d",&m,&n);
	for(int i=1;i<=m;i++)
	  for(int j=1;j<=n;j++)
	    scanf("%d",&g[i][j]);
	solve();
	return 0;
}

猜你喜欢

转载自blog.csdn.net/SongBai1997/article/details/83098780
今日推荐