【HDU】3279 - Fliptile(bfs & 二进制)

51 篇文章 1 订阅
11 篇文章 0 订阅

点击打开题目

Fliptile
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 7440 Accepted: 2797

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




差点wa到死的一道题,原因是习惯性的把它当成4*4的棋盘了(哭)。


正经的,说一下这个题的思路:

首先我们看(1,1)这个点,能改变它的有(1,1)(1,2)(2,1)三个点,我们要是枚举的话,一共就2^(N*M)种结果了,最差的就要枚举2^225,好可怕的数。

而第一行一共有w个数,每个数的状态为0或1,那么一共是2^w种,我们就先枚举这2^w种情况(用二进制)。

如果第一行翻转完成后,能改变第一行的数只有第二行的数了,因为第一行已经翻转完成,一个点翻转两次没有意义,所以,当第一行翻转完成后,第2~h行的翻转的棋子也能依次解决了(当第(i,j)个棋子为黑的时候,我们就翻转(i+1,j的棋子))。最后检查一下最后一行是否全是白字,如果是则答案就出来了。


代码如下:

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <stack>
using namespace std;
#define CLR(a,b) memset(a,b,sizeof(a))
int mapp[22][22];
int move_x[] = {0,0,1,-1};
int move_y[] = {1,-1,0,0};
int w,h;
struct node
{
	int x,y;
}t;
stack<node> s;
void change(int x,int y)
{
	mapp[x][y] = !mapp[x][y];
	for (int i = 0 ; i < 4 ; i++)
		mapp[x+move_x[i]][y+move_y[i]] = !mapp[x+move_x[i]][y+move_y[i]];
}
bool check()
{
	for (int i = 1 ; i <= w ; i++)
		if (mapp[h][i])
			return false;
	return true;
}
void bfs()
{
	for (int i = 1 ; i < h ; i++)
	{
		for (int j = 1 ; j <= w ; j++)
			if (mapp[i][j])
			{
				t.x = i+1;
				t.y = j;
				s.push(t);
				change(i+1,j);
			}
	}
}
int main()
{
	int res[22][22];
	bool ans;		//答案是否存在 
	while (~scanf ("%d %d",&h,&w))
	{
		for (int i = 1 ; i <= h ; i++)
		{
			for (int j = 1 ; j <= w ; j++)
				scanf ("%d",&mapp[i][j]);
		}
		ans = false;
		for (int i = 0 ; i < (1 << w) ; i++)		//枚举第一行的所有翻转(惯性思维把它当做4*4的了) 
		{
			int op = i;
			int pos = 1;
			while (op)
			{
				if (op & 1)
				{
					t.x = 1;
					t.y = pos;
					s.push(t);
					change(1,pos);
				}
				op >>= 1;
				pos++;
			}
			bfs();
			if (check())
			{
				ans = true;
				break;
			}
			else		//不可行的话再翻回来 
			{
				while (!s.empty())
				{
					t = s.top();
					s.pop();
					change(t.x,t.y);
				}
			}
		}
		if (ans)
		{
			CLR(res,0);
			while (!s.empty())
			{
				t = s.top();
				s.pop();
				res[t.x][t.y] = 1;
			}
			for (int i = 1 ; i <= h ; i++)
			{
				for (int j = 1 ; j < w ; j++)
					printf ("%d ",res[i][j]);
				printf ("%d\n",res[i][w]);
			}
		}
		else
			printf ("IMPOSSIBLE\n");
	}
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值