【枚举】POJ1753 Flip Game&POJ2965 The Pilots Brothers' refrigerator

Flip Game
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 28802 Accepted: 12460

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).

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

 

 

最先用BFS做暴搜直接超时=。=改为DFS勉强过了

 

DFS(POJ 313MS):

#include<stdio.h>

int dir[5][2]={1,0,0,1,-1,0,0,-1,0,0};
char map[10][10];
int flag,ans,step;
bool judge(){
	int i,j;
	for(i=1;i<=4;i++){
		for(j=1;j<=4;j++){
			if(map[i][j]!=map[1][1])return false;
		}
	}
	return true;
}

void change(int x,int y){
	int i;

	for(i=0;i<=4;++i){

		if(map[x+dir[i][0]][y+dir[i][1]]=='w')
			map[x+dir[i][0]][y+dir[i][1]]='b';
		else
			map[x+dir[i][0]][y+dir[i][1]]='w';

	}

}

void dfs(int x,int y,int deep){
	if(deep == step)
	{
		if(judge())
			flag = 1;
		return;
	}

	if(x>4||flag)
		return ;
	change(x,y);
	if(y<4)
		dfs(x,y+1,deep+1);
	else
		dfs(x+1,1,deep+1);
	change(x,y);
	if(y<4)
		dfs(x,y+1,deep);
	else
		dfs(x+1,1,deep);

}


int main(){


	int i,j;
	for(i=1;i<=4;i++){
		for(j=1;j<=4;j++){
			scanf("%c",&map[i][j]);
		}
		getchar();
	}

	flag=0;
	for(step = 0; step <= 16; step++)
	{
		dfs(1, 1, 0);
		if(flag)
			break;
	}
	if(flag==1)
		printf("%d\n",step);
	else
		printf("Impossible\n");

	return 0;
}



 

 因为同一个翻两次等于不翻,便可以枚举第一行的每一个翻情况(2^4=16种)

对于每一种情况,要让一排变为同一色,只需翻动不同色对应的下一排那个,直到将前3排变为同一色,若这时剩下第4排也全为前面的颜色则可以达到目的,记录此时翻过的次数。

对于16种情况依次判断,取最小的次数,若都不能达成,则输出Impossible


枚举 (POJ 32MS):

#include<stdio.h>
#include<string.h>
int dir[5][2]={1,0,0,1,-1,0,0,-1,0,0};

int ans,count;

struct map{
	char m[10][10];
	int step;
}g[16],temp;

bool judge(map a){
	int i,j;
	for(i=1;i<=4;i++){
		for(j=1;j<=4;j++){
			if(a.m[i][j]!=a.m[1][1])return false;
		}
	}
	return true;
}

void change(int x,int y,map *a){
	int i;
	a->step++;
	for(i=0;i<=4;++i){

		if(a->m[x+dir[i][0]][y+dir[i][1]]=='w')
			a->m[x+dir[i][0]][y+dir[i][1]]='b';
		else
			a->m[x+dir[i][0]][y+dir[i][1]]='w';

	}

}

void build (int y,map a){
	if(y>4){
		g[count++]=a;
		return ;
	}
	map temp=a;
	build(y+1,a);
	change(1,y,&temp);
	build(y+1,temp);
}

void flip(map temp){
	map a=temp;
	int i,j;
	for(i=2;i<=4;++i){
		for(j=1;j<=4;++j){
			if(a.m[i-1][j]=='b'){
				change(i,j,&a);
			}
		}
	}
	if(judge(a)&&a.step<ans)
		ans=a.step;
	a=temp;
	for(i=2;i<=4;++i){
		for(j=1;j<=4;++j){
			if(a.m[i-1][j]=='w'){
				change(i,j,&a);
			}
		}
	}
	if(judge(a)&&a.step<ans)
		ans=a.step;
}

int main(){
	map now;
	int i,j;
	for(i=1;i<=4;i++){
		for(j=1;j<=4;j++){
			scanf("%c",&now.m[i][j]);
		}
		getchar();
	}
	now.step=0;
	ans=20;
	count=0;
	build(1,now);
	for(i=0;i<16;++i){
		flip(g[i]);
	}
	if(ans!=20)
		printf("%d\n",ans);
	else
		printf("Impossible\n");

	return 0;
}



 POJ2965 The Pilots Brothers' refrigerator


DFS暴搜,枚举所有情况

#include<stdio.h>
#include<string.h>
char map[10][10];
int vis[10][10];
int flag,ans,step;  


void change(int row,int col)  
{  
    int i;  
    for(i=1; i<=4; i++)  
    {  
        map[i][col] = !map[i][col];  
        map[row][i] = !map[row][i];  
    }  
    map[row][col] = !map[row][col];  
} 

bool judge(){
	int i,j;
	for(i=1;i<=4;++i)
		for(j=1;j<=4;++j)
			if(map[i][j]==0)
				return false;
	return true;
}


void dfs(int x,int y,int deep){  
	int i,j;
	if(deep == step)  
	{  
		if(judge())  {
			flag = 1;  
			printf("%d\n",step);
			for(i=1;i<=4;i++){
				for(j=1;j<=4;j++){
					if(vis[i][j]==1)
						printf("%d %d\n",i,j);
				}
			}
		}
		return;  
	}  
  
    if(x>4||flag)  
        return ;
	vis[x][y]=1;
    change(x,y);  
    if(y<4)  
        dfs(x,y+1,deep+1);  
    else  
        dfs(x+1,1,deep+1);  
    change(x,y);  
	vis[x][y]=0;
    if(y<4)  
        dfs(x,y+1,deep);  
    else  
        dfs(x+1,1,deep);  
  
}  


		



int main(){  
  
	char c;
    int i,j;  
    for(i=1;i<=4;i++){  
        for(j=1;j<=4;j++){  
            scanf("%c",&c);  
			if(c=='-')  map[i][j]=1;   
            else        map[i][j]=0; 
        }  
        getchar();  
    }  
  
    flag=0;  
    for(step = 0; step <= 16; step++)  
    {	
		memset(vis,0,sizeof(vis));
        dfs(1, 1, 0);  
        if(flag)  
            break;  
    }  
    
  
    return 0;  
} 



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值