Fliptile POJ327二进制枚举

**

POJ327二进制枚举

**
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

题目大意:把所有1翻转为0 翻转某个数时会把周围的都翻转,也就是说除了最后一排所有的数字都会被下面的一排所影响,也就是可以通过下面一排的翻转完成上一排的翻转

对于某个位置来说 如果他的上一个位置是1, 就翻转他, 由他来完成他上一排的翻转

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<stack>
#include<queue>
#include<fstream>

using namespace std;
int m, n;
int ans, pos;
int cnt;
int map[20][20], a[20][20], cop[20][20];
int dx[4] = {0, 0, -1, 1};
int dy[4] = {-1, 1, 0, 0};
void flip(int i, int j){
	cnt++;
	a[i][j] = 1;//  记录翻转的瓷砖
	cop[i][j] = !cop[i][j]; // 先翻转自己
	for(int k = 0; k < 4; k++) // 寻找四个方向 
		if(i + dx[k] > -1 && j + dy[k] > -1)
			cop[i + dx[k]][j + dy[k]] ^= 1; // 是0就为1; 是1就为0 
} 
int find(int k ){ // 对于每一种情况判断是否可以产生最终结果  5 --- 1001 
	cnt = 0; // 步数 
	memcpy(cop, map, sizeof(cop)); // 初始化副本 
	for(int j = 0; j < m; j++) // 二进制压缩  如果j从0-3 则 1000 0100 0010 0001 
		if(k & (1 << (m - 1 - j))) // 对于每一个K的取值 如1010 找不到0 只需要翻转1
			flip(0,j); // 如果某一列不为0 就翻转第一行的这个位置 
			 
	for(int i = 1; i < n; i++)
		for(int j = 0; j < m; j++)
			if(cop[i - 1][j]) flip(i,j); // 如果上一排的这个位置是1 则这个位置需要翻一下
	
	for(int j = 0; j < m; j++)
	if(cop[n - 1][j]) return false; //所以行的1都可以由下一行搞定 但是最后一行的1没有下一行 所以考虑最后一行的1是否被搞定 
	return true; 
}
int main(){	
	while(~scanf("%d%d", &n, &m)){
		for(int i = 0; i < n; i++)
			for(int j = 0; j < m; j++)
				scanf("%d", &map[i][j]);
		ans = n * m + 1;
		pos = -1;
		for(int i = 0; i < (1<<m); i++)
			if(find(i) && cnt < ans){
				ans = cnt;
				pos = i;
			}
			
		memset(a, 0, sizeof(a));
		if(pos >= 0){
			find(pos);
			for(int i = 0; i < n; i++){
				for(int j = 0; j < m; j++){
					if(j < m - 1)
					printf("%d ", a[i][j]);
					else printf("%d\n", a[i][j]);
				}
			}
	
			
		}	else cout << "IMPOSSIBLE" << endl;	
	}
	
	return 0;
}
发布了28 篇原创文章 · 获赞 22 · 访问量 1107

猜你喜欢

转载自blog.csdn.net/qq_45432665/article/details/102963118