您的位置:首页 > 其它

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

2014-04-26 00:20 543 查看
[align=center]Flip Game[/align]

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:
Choose any one of the 16 pieces.

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  解题报告 枚举