您的位置:首页 > 其它

poj 1753 Flip Game

2015-08-23 18:32 405 查看
Flip Game

Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 34683Accepted: 15184
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


Source

Northeastern Europe 2000

注意到没有说多组测试数据,(实际肯定是多组的),证明不用写while( .... !=EOF)之类的,我就是因为这超时了

[cpp] view
plaincopy

#include<cstdio>

#include<string>

#include<cstring>

#include<iostream>

#include<cmath>

#include<algorithm>

#include<climits>

#include<queue>

#include<vector>

#include<map>

#include<sstream>

#include<set>

#include<stack>

#include<utility>

#pragma comment(linker, "/STACK:102400000,102400000")

#define PI 3.1415926535897932384626

#define eps 1e-10

#define sqr(x) ((x)*(x))

#define FOR0(i,n) for(int i=0 ;i<(n) ;i++)

#define FOR1(i,n) for(int i=1 ;i<=(n) ;i++)

#define FORD(i,n) for(int i=(n) ;i>=0 ;i--)

#define lson num<<1,le,mid

#define rson num<<1|1,mid+1,ri

#define MID int mid=(le+ri)>>1

#define zero(x)((x>0? x:-x)<1e-15)

#define mp make_pair

#define _f first

#define _s second



using namespace std;

const int INF =0x3f3f3f3f;

//const int maxn= ;

//const int maxm= ;

//const int INF= ;

typedef long long ll;

const ll inf =(ll)1000000000000000;//1e15;

//ifstream fin("input.txt");

//ofstream fout("output.txt");

//fin.close();

//fout.close();

//freopen("a.in","r",stdin);

//freopen("a.out","w",stdout);

//by yskysker123

我的代码:朴素的dfs

[cpp] view
plaincopy

int a[6][6];

const int n=4,m=4;

int ans;

int dir[4][2]={ {-1,0},{+1,0},{0,-1},{0,+1} };

bool in(int &y, int &x)

{

return 1<=y&&y<=n&&1<=x&&x<=m;

}

void dfs(int id,int step)

{

if(id==17)

{

int sum=0;

for(int i=1;i<=n;i++)

{

for(int j=1;j<=m;j++)

{

sum+=a[i][j];

}

}

if(sum==0||sum==16) ans=min(ans,step);

return ;



}

int y=(id-1)/4 +1;

int x= id%4 ==0?4: id%4 ;

// cout<<y<<" "<<x<<endl;



dfs(id+1,step);



a[y][x]^=1;

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

{

int ty=y+dir[i][0];

int tx=x+dir[i][1];

if(!in(ty,tx)) continue;

a[ty][tx]^=1;

}



dfs(id+1,step+1);

a[y][x]^=1;

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

{

int ty=y+dir[i][0];

int tx=x+dir[i][1];

if(!in(ty,tx)) continue;

a[ty][tx]^=1;

}



}

int main()

{

char ch;

while(1)

{

ans=INF;

FOR1(i,n)

{

FOR1(j,m)

{

if(scanf(" %c",&ch)==EOF )

break;

a[i][j]=ch=='w'?0:1;

// cout<<a[i][j]<<endl;

}

}

dfs( 1,0);

if(ans==INF) {puts("Impossible") ;continue;}

printf("%d\n",ans );

}







return 0;

}

看了别人的代码,发现可以对深度进行限制,然后逐步递增,这样就既可以保证最小,又可以免去朴素dfs找到真正结果后不必要的后续验证(是否是最小)。

但是实际上这种方法比朴素dfs慢了一倍,200ms左右,我认为是数据不大的缘故,不必要的验证并未花费太多时间。(层数还太少)

另外就是可以从全部是白或全部是黑为起点进行搜索,把所有可能的状态存下来,我并没有验证这种方法是否可行。

[cpp] view
plaincopy

int a[6][6];

const int n=4,m=4;

int ans,deep;

bool flag;

int dir[4][2]={ {-1,0},{+1,0},{0,-1},{0,+1} };

bool in(int &y, int &x)

{

return 1<=y&&y<=n&&1<=x&&x<=m;

}

void dfs(int id,int step)

{

if(step==deep)

{

int sum=0;

for(int i=1;i<=n;i++)

{

for(int j=1;j<=m;j++)

{

sum+=a[i][j];

}

}

if(sum==0||sum==16) flag=1;

return ;



}

if(flag||id==17) return ;

int y=(id-1)/4 +1;

int x= id%4 ==0?4: id%4 ;

// cout<<y<<" "<<x<<endl;



dfs(id+1,step);



a[y][x]^=1;

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

{

int ty=y+dir[i][0];

int tx=x+dir[i][1];

if(!in(ty,tx)) continue;

a[ty][tx]^=1;

}



dfs(id+1,step+1);

a[y][x]^=1;

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

{

int ty=y+dir[i][0];

int tx=x+dir[i][1];

if(!in(ty,tx)) continue;

a[ty][tx]^=1;

}



}

int main()

{

char ch;





ans=INF;

flag=0;

FOR1(i,n)

{

FOR1(j,m)

{

if(scanf(" %c",&ch)==EOF )

break;

a[i][j]=ch=='w'?0:1;

}

}

for(int i=0;i<=16;i++)

{

deep=i;

dfs( 1,0);

if(flag) {ans=i;break;}

}



if(ans==INF) puts("Impossible") ;

else printf("%d\n",ans );









return 0;

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