poj1753 Flip Game DFS,枚举

Flip Game
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 34437   Accepted: 15058

Description

Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of its 16 squares. One side of each piece is white and the other one is black and each piece is lying either it's black or white side up. Each round you flip 3 to 5 pieces, thus changing the color of their upper side from black to white and vice versa. The pieces to be flipped are chosen every round according to the following rules:
  1. Choose any one of the 16 pieces.
  2. Flip the chosen piece and also all adjacent pieces to the left, to the right, to the top, and to the bottom of the chosen piece (if there are any).

poj1753 Flip Game DFS,枚举_第1张图片Consider the following position as an example:

bwbw
wwww
bbwb
bwwb
Here "b" denotes pieces lying their black side up and "w" denotes pieces lying their white side up. If we choose to flip the 1st piece from the 3rd row (this choice is shown at the picture), then the field will become:

bwbw
bwww
wwwb
wwwb
The goal of the game is to flip either all pieces white side up or all pieces black side up. You are to write a program that will search for the minimum number of rounds needed to achieve this goal.

Input

The input consists of 4 lines with 4 characters "w" or "b" each that denote game field position.

Output

Write to the output file a single integer number - the minimum number of rounds needed to achieve the goal of the game from the given position. If the goal is initially achieved, then write 0. If it's impossible to achieve the goal, then write the word "Impossible" (without quotes).

Sample Input

bwwb
bbwb
bwwb
bwww

Sample Output

4

#include<iostream>
#include<cstdio>
#define INF 0x3f3f3f3f

using namespace std;

string s[4];
int num=INF;
int a[10][10];
int panduan()
{
	for(int i=0;i<4;i++)
		for(int j=0;j<4;j++)
		if(a[i][j]!=a[0][0])
		return 0;
	return 1;
}
int fanzhuan(int x,int y)
{
	a[x][y]=!a[x][y];
	if(x<3)
		a[x+1][y]=!a[x+1][y];
	if(x>0)
		a[x-1][y]=!a[x-1][y];
	if(y>0)
		a[x][y-1]=!a[x][y-1];
	if(y<3)
		a[x][y+1]=!a[x][y+1];
}
int DFS(int x,int y,int ans)
{
	
	if(panduan())
		{if(num>ans)
		num=ans;
		return 0;
		}
	if(x>3||y>3)
		return 0;
	int fx,fy;
	fx=(x+1)%4;    //按列来翻转
	fy=(x+1)/4+y;
                          //每个位置都有2种情况 全部跑一遍
	fanzhuan(x,y);    //翻成另一面
	DFS(fx,fy,ans+1);

	fanzhuan(x,y);     //还原
	DFS(fx,fy,ans);
}
int main()
{
	while(cin>>s[0])
	{
		for(int i=1;i<4;i++)
		cin>>s[i];
		for(int i=0;i<4;i++)
			for(int j=0;j<4;j++)
			if(s[i][j]=='w')
			a[i][j]=1;
			else
				a[i][j]=0;
		DFS(0,0,0);
		if(num!=INF)
			cout<<num;
		else
			cout<<"Impossible";
		cout<<endl;
	}
}



<span id="transmark">DFS+枚举,时间相对少些</span>
#include<iostream>
#include<cstring>

using namespace std;

int num=0x3f3f3f3f;
int a[13][15],flag;
int panduan ()
{
	for(int i=0;i<4;i++)
		for(int j=0;j<4;j++)
		if(a[i][j]!=a[0][0])
		return 0;
	return 1;
}
int fanzhuan(int x,int y)
{
	a[x][y]=!a[x][y];
	if(x>0)
		a[x-1][y]=!a[x-1][y];
	if(x<3)
		a[x+1][y]=!a[x+1][y];
	if(y<3)
		a[x][y+1]=!a[x][y+1];
	if(y>0)
		a[x][y-1]=!a[x][y-1];
}
int DFS(int x,int y,int ans)
{
	if(num==ans)
	{flag=panduan();
	return 0;
	}
	if(x>3||y>3||flag)
	return 0;

	int fx=(x+1)%4;
	int fy=(x+1)/4+y;
      fanzhuan(x,y);       //此处顺序不能变
	DFS(fx,fy,ans+1);

	fanzhuan(x,y);
	DFS(fx,fy,ans);

	return 0;
}
int main()
{
    string s[4];
	for(int i=0;i<4;i++)
		cin>>s[i];

	for(int i=0;i<4;i++)
		for(int j=0;j<4;j++)
		if(s[i][j]=='w')
		a[i][j]=1;
		else
			a[i][j]=0;
			flag=0;
        for(int i=0;i<=16;i++)  //枚举,..最多16次
		{
			num=i;
			DFS(0,0,0);
			if(flag)
				break;
		}
		if(flag)
			cout<<num;
		else
			cout<<"Impossible";
			cout<<endl;
}



你可能感兴趣的:(poj1753 Flip Game DFS,枚举)