POJ 3279 Fliptile(二进制搜索)

Fliptile


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

题意:有一个n*m的格子,每个格子都有黑白两面(0表示白色,1表示黑色)。我们需要把所有的格子都反转成黑色,每反转一个格子,它上下左右的格子都会跟着反转。请求出用最小步数完成反转时每个格子反转的次数。有多个解时,输出字典序最小的一组。

解题思路:枚举第一行的所有可能的翻转情况,依次求解2~m行,若第m行也满足要求,即可输出,注意:一定要注意字典序,先判断n位,最后再判断第一位。

二进制搜索

#include<iostream>
#include<string.h>
using namespace std;
int a[20][20];//输入的矩阵
int b[20][20];//判断处理后的矩阵是否满足题意 
int dp[20][20];//用来记录翻转的次数 
int m,n;
int mov[4][2]={{0,1},{0,-1},{-1,0},{1,0}}; 
void unit(){//初始化 
	for(int i=1;i<=m;i++){
		for(int j=1;j<=n;j++){
			b[i][j]=a[i][j]; 
		}
	}
} 
void change(int b[][20],int i,int j){//改变与之相邻的数组的值 
    dp[i][j]=1;
    b[i][j]=((b[i][j]^1));
	for(int q=0;q<4;q++){
		int xx=i+mov[q][0];
		int yy=j+mov[q][1];
		if(xx>=1&&xx<=m&&yy>=1&&yy<=n){
			b[xx][yy]=(b[xx][yy]^1);//达到翻转为0 
		}
	}
}
int check(int i){//检查此行是否满足要求 
     for(int j=1;j<=n;j++){
     	if(b[i][j]==1){
     		return false;
		 }
	 }
	 return true; 
} 
int main(){
	int flag=0;
	scanf("%d%d",&m,&n);//m行n列 
	for(int i=1;i<=m;i++){
		for(int j=1;j<=n;j++){
			scanf("%d",&a[i][j]);
		}
	}
	for(int i=0;i<(1<<n);i++){
		unit();
		memset(dp,0,sizeof(dp));
		for(int v=n;v>=1;v--){//第一行的状态是否需要改变
			if(i&(1<<(v-1))){//与运算判断第i位是否需要翻转 
				change(b,1,v);
			}
		}
	    for(int j=2;j<=m;j++){//翻转2~m行 
		   for(int k=n;k>=1;k--){
			 if(b[j-1][k]==1){//观察上一行的情况,以此确定是否需要翻转 
				change(b,j,k);
			}
		}
	}
		if(check(m)){
			for(int i=1;i<=m;i++){
				for(int j=1;j<=n;j++){
					if(j!=1){
						cout<<" ";
					}
					cout<<dp[i][j];
				}
				cout<<endl;
			}
			flag=1;
	     return 0;
	 }
	} 
    if(!flag)
		cout<<"IMPOSSIBLE"<<endl;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/amf12345/article/details/89088187
今日推荐