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

题意:给定一个0,1矩阵,要求用最少的次数将矩阵翻转为全是0。每次翻转一个值时周围4个和自己一起翻转。
解析:一看并没有发现和搜索有什么关系,然后去看了一下别人解析,发现确实和搜索没有什么很大关系。。。暴力枚举,因为我们如果一行行往下翻转的话,会发现只要上面的硬币是1,那么这个硬币就必须翻转,否则不能翻转。那这样的话,我们只要枚举出第一行所有的状态,那么下面每一行的状态我们就知道了。因为最后一行是由倒数第二行决定的,所以最后一行我们无法保证都是0,那我们最后进行检测,如果最后一行都是0,则方案可取,否则不可取。最后得出翻转最少的状态。我的第一行所有的状态和下面对应每一行的状态是用搜索写的,不用也可以。

#include<iostream>
#include<cstring>
using namespace std;
int m,n;
int sum=100000000;
int a[17][17];                 //用来储存开始的矩阵
bool b[17][17];                //表示当前位置是否翻转,1表示翻转
bool c[17][17];                //用来储存最后输出的翻转状态
int judge() {                                 //判断最后一行是否全为0
	for(int i=1; i<=n; i++)
		if((a[m][i]+b[m][i]+b[m][i-1]+b[m][i+1]+b[m-1][i])%2==1)
			return 0;
	return 1;
}
void dfs(int k,int num) {                  //再已知第一行的状态下找出下面每一行的状态
	if(num>=sum)
		return ;
	if(k>m) {
		if(judge()) {
			memcpy(c,b,sizeof(b));
			sum=num;
		}
		return ;
	}
	for(int i=1; i<=n; i++) {
      if((a[k-1][i]+b[k-1][i-1]+b[k-1][i]+b[k-1][i+1]+b[k-2][i])%2==1) {//判断上一行此位置现在是否为1
			b[k][i]=1;
			num++;
		} else
			b[k][i]=0;
	}
	dfs(k+1,num);

}
void first_line(int k,int num) {       //用来枚举第一行所有的状态
	if(k>n) {
		dfs(2,num);
		return ;
	}
	b[1][k]=0;
	first_line(k+1,num);
	b[1][k]=1;
	first_line(k+1,num+1);
}
int main() {
	cin>>m>>n;
	for(int i=1; i<=m; i++)
		for(int j=1; j<=n; j++)
			cin>>a[i][j];
	memset(b,0,sizeof(b));
	first_line(1,0);
	if(sum==100000000)
		cout<<"IMPOSSIBLE";
	else
		for(int i=1; i<=m; i++) {
			for(int j=1; j<n; j++)
				cout<<c[i][j]<<" ";
			cout<<c[i][n]<<endl;
		}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值