POJ3279-Fliptile【反转问题】

原题链接
Fliptile
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 7990 Accepted: 2990
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

题意:对于一个给定的黑白色的矩阵,奶牛每次反转可以反转一个格子和其上下左右的格子。输出最小的反转次数时的操作的矩阵表示。如果没有就输出"IMPOSSIBLE"。
思路:乍一看好像没有思路,其实我们需要对这个问题进行分析。首先一个格子翻不翻就代表这个问题最简单的解法就是枚举出所有可能的翻牌的情况。但是这样明显会超时所以我们需要其他的办法。对于反转的问题,我们知道反转的顺序不会影响最后的结果。那么如果我们从最上面往最下面开始反转呢,这样我们不就是可以确定当前的这个格子是否需要反转了吗?但是有一个问题就是第一行并不存在“上一行”,所以我们需要模拟第一行的所有的反转情况。这样到了最后一行的时候如果最后一行和第一行都是白的那么明显就符合要求。如果这个矩阵中仍然存在着没有翻过来的格子那么久说明这个方案是不可行的。
代码如下:

//http://poj.org/problem?id=3279
#include <algorithm>
#include <iostream>
#include <utility>
#include <sstream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <map>
#include <set>
using namespace std;

typedef long long ll;
const int MOD = int(1e9) + 7;
//int MOD = 99990001;
const int INF = 0x3f3f3f3f;
const ll INFF = (~(0ULL)>>1);
const double EPS = 1e-9;
const double OO = 1e20;
const double PI = acos(-1.0); //M_PI;
const int fx[] = {-1, 1, 0, 0, 0};
const int fy[] = {0, 0, 0, -1, 1};
const int maxn=15 + 5;
int m,n;//代表m行n列的意思
int a[maxn][maxn];//这就是输入的方格的颜色,0代表白色,1代表黑色,最后要全部变白
int res[maxn][maxn];//这是用来存储最后的答案的
int t[maxn][maxn];//这是中间用来过渡的答案,里面记录的是是否翻转的信息
bool can(int x,int y){
	if(x >= 0 && x <m && y >= 0 && y < n) return true;
	else return false;
}
//判断当前位置的颜色
int get(int x,int y){
	int sum=a[x][y];
	for(int i=0;i<5;i++){
		int newx=x+fx[i],newy=y+fy[i];
		if(can(newx,newy) && t[newx][newy]!=0){
			sum++;
		}
	}
	// cout << sum << endl;
	return sum & 1;
}
//如果在当前的第一行的翻转情况下,不能够全变白返回-1,否则返回最后的所需的步数
int calc(){
	//从第二行开始对后面的每一行进行翻转
	for(int i=1;i<m;i++){
		for(int j=0;j<n;j++){
			if(get(i-1,j)!=0){//如果当前位置的上面需要翻转那么就翻转
				t[i][j]=1;
			}
		}
	}
	//判断一下最后一行是否可行
	for(int i=0;i<n;i++){
		if(get(m-1,i) != 0){
			return -1;
		}
	}
	//到了这一步说明最少他是一个解,所以需要计算出所有的翻转次数
	int ans=0;
	for(int i=0;i<m;i++){
		for(int j=0;j<n;j++){
			ans+=t[i][j];
		}
	}
	return ans;
}

int main(){
		cin >> m >> n;
		for(int i=0;i<m;i++){
			for(int j=0;j<n;j++){
				scanf("%d",&a[i][j]);
			}
		}
		int ans=-1;//初值定义成-1就是用来判断最后是否有答案的
		//接下来列举第一行所有的翻转情况
		for(int i=0;i < 1 << n;i++){
			memset(t,0,sizeof(t));
			for(int j=0;j<n;j++){
				t[0][n-j-1] = i >> j & 1;//这里是t[0][n-j-1]的原因就是需要最后在相同步数的情况下输出最小的字典序
			}
			// for(int j=0;j<n;j++) cout << t[0][j] << " ";
				// cout << endl;
			int num=calc();
			// cout << num << endl;
			if(num >= 0 && (ans == -1 || num < ans)){
				ans = num;
				for(int i=0;i<m;i++){
					for(int j=0;j<n;j++){
						res[i][j]=t[i][j];
					}
				}
			}
		}
		if(ans == -1) cout << "IMPOSSIBLE" << endl;
		else{
			for(int i=0;i<m;i++){
				for(int j=0;j<n-1;j++){
					cout << res[i][j] << " ";
				}
				cout << res[i][n-1] << endl;
			}
		}
        return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

门豪杰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值