您的位置:首页 > 其它

POJ 1753 Flip Game (DFS)

2015-01-21 20:27 495 查看
Flip Game

Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 32104Accepted: 13988
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).

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<stdlib.h>
#include<queue>
#include<stack>
#include<algorithm>
#define LL __int64
using namespace std;
const int MAXN=5+5;
const int INF=0x3f3f3f3f;
const double EPS=1e-9;
int dir4[][2]={{0,1},{1,0},{0,-1},{-1,0}};
int dir8[][2]={{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1},{-1,0},{-1,1}};
int dir_8[][2]={{-2,1},{-1,2},{1,2},{2,1},{2,-1},{1,-2},{-1,-2},{-2,-1}};
int map[6][6],step;
bool flag;
char temp;

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

void turn(int x,int y)
{
map[x][y]=!map[x][y];
for(int i=0;i<4;i++)
{
int xx=x+dir4[i][0];
int yy=y+dir4[i][1];
map[xx][yy]=!map[xx][yy];
}
}

void DFS(int x,int y,int cur)
{
if(cur==step)
{
flag=judge();
return ;
}
if(x==4||flag) return ;

turn(x,y);
if(y==3)
DFS(x+1,0,cur+1);
else
DFS(x,y+1,cur+1);

turn(x,y);
if(y==3)
DFS(x+1,0,cur);
else
DFS(x,y+1,cur);

return ;
}
int main()
{
//freopen("in.txt","r",stdin);
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
scanf("%c",&temp);
if(temp=='b')
map[i][j]=1;
}
getchar();
}

flag=false;
for(step=0;step<=16;step++)
{
DFS(0,0,0);
if(flag) break;
}

if(flag)
cout<<step<<endl;
else
cout<<"Impossible"<<endl;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: