POJ 3279 Fliptile(二进制枚举、递推、POJ 1426 Find The Multiple、详解)

POJ 3279 Fliptile

测试平台
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


题目难点

问题:奶牛对(x,y)的瓷砖进行翻盖会同时影响周围的瓷砖,如何才能最快的确定最佳方案(全部瓷砖都为白色)?

①、初步思路
暴力枚举。当 N=15,M=15时,有情况数是2(15*15),时间复杂度过高。

②、思维变化
枚举+递推。枚举出第一行可能出现的情况,然后通过上一行的情况递推出下一行翻转情况,最后验证结果。


具体方案

1、二进制枚举
每块瓷砖有两种变化:黑、白。利用这种性质我们可以采用二进制枚举。
第一行最多的情况数有 215 时间复杂度上满足条件。

//二进制枚举第一行翻转情况代码
//test二维数组:递推每块瓷砖的翻转次数
for(int i=0; i<(1<<m); i++){
     
	memset(test, 0, sizeof(test));
	for(int j=0; j<m; j++)
		test[0][m-j-1] = (i>>j)&1;
	}

①、"<<" 左移运算
枚举出第一行翻转的情况数 2m,在 [0,2m-1) 的区间上,每一个数都代表着一种第一行的翻转情况。

②、">>"右移运算 与 "&"
x >> j:将 x 的二进制右移 j 位
例如:1010 >> 1 = 101
x & 1:确定 x 在二进制下的最低位。
例如:1010 & 1 = 0,101 & 1 = 1

值得注意的是这里我从后往前枚举,为保证字典序最小的先枚举到。


2、递推
首先我们确定了第一行的瓷砖的黑白情况,在递推出下一行的黑白情况。
例如(0, 1)是黑色的瓷砖,那么(1, 1)的瓷砖必须要翻转,因为在接下来的递推过程中只有翻转(1,1)的才能改变(0, 1)的瓷砖颜色。

//判断(x, y)的瓷砖的颜色
int judge(int x, int y){
     
	int tmp = file[x][y];
	for(int i=0; i<5; i++){
     
		int ddx = x+dx[i];
		int ddy = y+dy[i];
		if(ddx>=0&&ddx<n&&ddy>=0&&ddy<m)
			tmp += test[ddx][ddy];
	}
	
	return tmp%2;
}
//递推
for(int i=1; i<n; i++)
	for(int j=0; j<m; j++)
	if(judge(i-1, j))
		test[i][j]=1;

3、验证
当我们递推完最后一行的翻转情况时,我们只需要验证最后一行是否为全白即可。
若为全白则由当前第一行情况递推出的翻转情况就是一种解。

//验证
for(int i=0; i<m; i++)
if(judge(n-1, i)) 
	return -1;

实现代码

#include
#include
#define INF 0x3f3f3f3f
int n, m;

//瓷砖初始化状态、测试翻转方案、最优解方案
int file[20][20], test[20][20], ans[20][20];

//邻接瓷砖
int dx[5]={
     0, 0, 0, 1, -1};
int dy[5]={
     -1, 1, 0, 0, 0};

//判断(x, y)的瓷砖的颜色
int judge(int x, int y){
     
	int c = file[x][y];
	for(int i=0; i<5; i++){
     
		int ddx = x+dx[i];
		int ddy = y+dy[i];
		if(ddx>=0&&ddx<n&&ddy>=0&&ddy<m)
			c += test[ddx][ddy];
	}
	
	return c%2;
}

//在第一行确定下,判断是否有解
//若有解则统计操作数
int sum(){
     
	for(int i=1; i<n; i++)
	for(int j=0; j<m; j++)
	if(judge(i-1, j))
		test[i][j]=1;
	
	//判断是否有解
	for(int i=0; i<m; i++)
	if(judge(n-1, i)) 
		return -1;
	
	//统计操作数
	int total=0;
	for(int i=0; i<n; i++)
	for(int j=0; j<m; j++)
	total += test[i][j];
	
	return total;
}

//枚举第一行、输出最优解
void slove(){
     
	int res = INF;
	
	//二进制枚举第一行的情况
	for(int i=0; i<(1<<m); i++){
     
		memset(test, 0, sizeof(test));
		for(int j=0; j<m; j++)
			test[0][m-j-1] = (i>>j)&1;
		
		int num = sum();
		//判断是否有更优解
		if(num>=0&&num<res){
     
			res = num;
			memcpy(ans, test, sizeof(test));
		}
	}
	
	//无解
	if(res == INF)
		printf("IMPOSSIBLE\n");
	//有解
	else
		for(int i=0; i<n; i++){
     
			for(int j=0; j<m; j++){
     
				if(j!=0)
					printf(" ");
				printf("%d", ans[i][j]);
			}
			printf("\n");
		}
		
	return;
}

int main(){
     
	while(scanf("%d%d", &n, &m)==2){
     
		for(int i=0; i<n; i++)
		for(int j=0; j<m; j++)
			scanf("%d", &file[i][j]);
			
		slove();
	}
	
	return 0;
} 

二进制枚举 拓展题目 POJ 1426 Find The Multiple

测试平台
Description

Given a positive integer n, write a program to find out a nonzero multiple m of n whose decimal representation contains only the digits 0 and 1. You may assume that n is not greater than 200 and there is a corresponding m containing no more than 100 decimal digits.
Input

The input file may contain multiple test cases. Each line contains a value of n (1 <= n <= 200). A line containing a zero terminates the input.
Output

For each value of n in the input print a line containing the corresponding value of m. The decimal representation of m must not contain more than 100 digits. If there are multiple solutions for a given value of n, any one of them is acceptable.
Sample Input

2
6
19
0
Sample Output

10
100100100100100100
111111111111111111


实现代码

#include
#define ll long long
ll n, m;

void f(){
     
	for(ll i=1; i<=(1<<20); i++){
     
		m=0;
		for(int j=20; j>=0; j--){
     
			m += (i>>j)&1;
			if(j!=0)
				m *= 10;
		}
		if(m%n==0&&m!=0) {
     
			printf("%lld\n", m);
			return;
		}
	}
	
	return ;
}

int main(){
     
	while(scanf("%d", &n)==1){
     
		if(n==0) break;
		f();
	}
	
	return 0;
} 

如果文章对你有帮助,给我个赞哈 :)

你可能感兴趣的:(基础算法题,算法,递推,经验分享)