HDU-3309-Roll The Cube(BFS)

来源:互联网 发布:福建游龙网络 编辑:程序博客网 时间:2024/05/16 11:16
Problem Description
This is a simple game.The goal of the game is to roll two balls to two holes each.
'B' -- ball
'H' -- hole
'.' -- land
'*' -- wall
Remember when a ball rolls into a hole, they(the ball and the hole) disappeared, that is , 'H' + 'B' = '.'.
Now you are controlling two balls at the same time.Up, down , left , right --- once one of these keys is pressed, balls exist roll to that direction, for example , you pressed up , two balls both roll up.
A ball will stay where it is if its next point is a wall, and balls can't be overlap.
Your code should give the minimun times you press the keys to achieve the goal.
 

Input
First there's an integer T(T<=100) indicating the case number.
Then T blocks , each block has two integers n , m (n , m <= 22) indicating size of the map.
Then n lines each with m characters.
There'll always be two balls(B) and two holes(H) in a map.
The boundary of the map is always walls(*).
 

Output
The minimum times you press to achieve the goal.
Tell me "Sorry , sir , my poor program fails to get an answer." if you can never achieve the goal.
 

Sample Input
46 3****B**B**H**H****4 4*****BB**HH*****4 4*****BH**HB*****5 6*******.BB***.H*H**..*.*******
 

Sample Output
312Sorry , sir , my poor program fails to get an answer.
 

Author
MadFroG
 

Source
HDOJ Monthly Contest – 2010.02.06 


思路:普通的BFS,就是球移动的时候处理有点麻烦,要分还剩一个球和还剩两个球的情况考虑。


#include <stdio.h>#include <string.h>struct S{int x[2],y[2],hx[2],hy[2],step,num;}que[1000000],t;int n,m;char mp[25][25];bool vis[25][25][25][25];int main(){    int T,i,j,bf,hf;    scanf("%d",&T);    while(T--)    {        scanf("%d%d",&n,&m);        for(i=1;i<=n;i++) scanf("%s",mp[i]+1);        bf=hf=0;        for(i=1;i<=n;i++)        {            for(j=1;j<=m;j++)            {                if(mp[i][j]=='B')                {                    que[0].x[bf]=i;                    que[0].y[bf]=j;                    bf++;                    mp[i][j]='.';                }                if(mp[i][j]=='H')                {                    que[0].hx[hf]=i;                    que[0].hy[hf]=j;                    hf++;                    mp[i][j]='.';                }            }        }        int top=0;        int bottom=1;        que[0].step=0;        que[0].num=2;        memset(vis,0,sizeof vis);        while(top<bottom)        {            t=que[top];            if(!t.num)            {                printf("%d\n",t.step);                break;            }            if(t.num==2)            {                if(t.x[0]<t.x[1])//x-1                {                    if(t.x[0]-1>=1 && mp[t.x[0]-1][t.y[0]]!='*') t.x[0]--;                    for(i=0;i<2;i++) if(t.hx[i]==t.x[0] && t.hy[i]==t.y[0])                    {                        t.hx[i]=t.x[0]=0;                        t.num--;                    }                    if(t.x[1]-1>=1 &&mp[t.x[1]-1][t.y[1]]!='*' && !(t.x[1]-1==t.x[0] && t.y[1]==t.y[0])) t.x[1]--;                    for(i=0;i<2;i++) if(t.hx[i]==t.x[1] && t.hy[i]==t.y[1])                    {                        t.hx[i]=t.x[1]=0;                        t.num--;                    }                    if(!vis[t.x[0]][t.y[0]][t.x[1]][t.y[1]])                    {                        vis[t.x[0]][t.y[0]][t.x[1]][t.y[1]]=1;                        if(t.num==1 && !t.x[0])                        {                            t.x[0]=t.x[1];                            t.y[0]=t.y[1];                            t.x[1]=0;                        }                        t.step++;                        que[bottom++]=t;                    }                }                else                {                    if(t.x[1]-1>=1 && mp[t.x[1]-1][t.y[1]]!='*') t.x[1]--;                    for(i=0;i<2;i++) if(t.hx[i]==t.x[1] && t.hy[i]==t.y[1])                    {                        t.hx[i]=t.x[1]=0;                        t.num--;                    }                    if(t.x[0]-1>=1 &&mp[t.x[0]-1][t.y[0]]!='*' && !(t.x[1]==t.x[0]-1 && t.y[1]==t.y[0])) t.x[0]--;                    for(i=0;i<2;i++) if(t.hx[i]==t.x[0] && t.hy[i]==t.y[0])                    {                        t.hx[i]=t.x[0]=0;                        t.num--;                    }                    if(!vis[t.x[0]][t.y[0]][t.x[1]][t.y[1]])                    {                        vis[t.x[0]][t.y[0]][t.x[1]][t.y[1]]=1;                        if(t.num==1 && !t.x[0])                        {                            t.x[0]=t.x[1];                            t.y[0]=t.y[1];                            t.x[1]=0;                        }                        t.step++;                        que[bottom++]=t;                    }                }//x-1                t=que[top];                if(t.x[0]>t.x[1])//x+1                {                    if(t.x[0]+1<=n && mp[t.x[0]+1][t.y[0]]!='*') t.x[0]++;                    for(i=0;i<2;i++) if(t.hx[i]==t.x[0] && t.hy[i]==t.y[0])                    {                        t.hx[i]=t.x[0]=0;                        t.num--;                    }                    if(t.x[1]+1<=n && mp[t.x[1]+1][t.y[1]]!='*' && !(t.x[1]+1==t.x[0] && t.y[1]==t.y[0])) t.x[1]++;                    for(i=0;i<2;i++) if(t.hx[i]==t.x[1] && t.hy[i]==t.y[1])                    {                        t.hx[i]=t.x[1]=0;                        t.num--;                    }                    if(!vis[t.x[0]][t.y[0]][t.x[1]][t.y[1]])                    {                        vis[t.x[0]][t.y[0]][t.x[1]][t.y[1]]=1;                        if(t.num==1 && !t.x[0])                        {                            t.x[0]=t.x[1];                            t.y[0]=t.y[1];                            t.x[1]=0;                        }                        t.step++;                        que[bottom++]=t;                    }                }                else                {                    if(t.x[1]+1<=n && mp[t.x[1]+1][t.y[1]]!='*') t.x[1]++;                    for(i=0;i<2;i++) if(t.hx[i]==t.x[1] && t.hy[i]==t.y[1])                    {                        t.hx[i]=t.x[1]=0;                        t.num--;                    }                    if(t.x[0]+1<=n &&mp[t.x[0]+1][t.y[0]]!='*' && !(t.x[1]==t.x[0]+1 && t.y[1]==t.y[0])) t.x[0]++;                    for(i=0;i<2;i++) if(t.hx[i]==t.x[0] && t.hy[i]==t.y[0])                    {                        t.hx[i]=t.x[0]=0;                        t.num--;                    }                    if(!vis[t.x[0]][t.y[0]][t.x[1]][t.y[1]])                    {                        vis[t.x[0]][t.y[0]][t.x[1]][t.y[1]]=1;                        if(t.num==1 && !t.x[0])                        {                            t.x[0]=t.x[1];                            t.y[0]=t.y[1];                            t.x[1]=0;                        }                        t.step++;                        que[bottom++]=t;                    }                }//x+1                t=que[top];                if(t.y[0]>t.y[1])//y+1                {                    if(t.y[0]+1<=m && mp[t.x[0]][t.y[0]+1]!='*') t.y[0]++;                    for(i=0;i<2;i++) if(t.hx[i]==t.x[0] && t.hy[i]==t.y[0])                    {                        t.hx[i]=t.x[0]=0;                        t.num--;                    }                    if(t.y[1]+1<=m && mp[t.x[1]][t.y[1]+1]!='*' && !(t.x[1]==t.x[0] && t.y[1]+1==t.y[0])) t.y[1]++;                    for(i=0;i<2;i++) if(t.hx[i]==t.x[1] && t.hy[i]==t.y[1])                    {                        t.hx[i]=t.x[1]=0;                        t.num--;                    }                    if(!vis[t.x[0]][t.y[0]][t.x[1]][t.y[1]])                    {                        vis[t.x[0]][t.y[0]][t.x[1]][t.y[1]]=1;                        if(t.num==1 && !t.x[0])                        {                            t.x[0]=t.x[1];                            t.y[0]=t.y[1];                            t.x[1]=0;                        }                        t.step++;                        que[bottom++]=t;                    }                }                else                {                    if(t.y[1]+1<=m && mp[t.x[1]][t.y[1]+1]!='*') t.y[1]++;                    for(i=0;i<2;i++) if(t.hx[i]==t.x[1] && t.hy[i]==t.y[1])                    {                        t.hx[i]=t.x[1]=0;                        t.num--;                    }                    if(t.y[0]+1<=m &&mp[t.x[0]][t.y[0]+1]!='*' && !(t.x[1]==t.x[0] && t.y[1]==t.y[0]+1)) t.y[0]++;                    for(i=0;i<2;i++) if(t.hx[i]==t.x[0] && t.hy[i]==t.y[0])                    {                        t.hx[i]=t.x[0]=0;                        t.num--;                    }                    if(!vis[t.x[0]][t.y[0]][t.x[1]][t.y[1]])                    {                        vis[t.x[0]][t.y[0]][t.x[1]][t.y[1]]=1;                        if(t.num==1 && !t.x[0])                        {                            t.x[0]=t.x[1];                            t.y[0]=t.y[1];                            t.x[1]=0;                        }                        t.step++;                        que[bottom++]=t;                    }                }//y+1                t=que[top];                if(t.y[0]<t.y[1])//y-1                {                    if(t.y[0]-1>=1 && mp[t.x[0]][t.y[0]-1]!='*') t.y[0]--;                    for(i=0;i<2;i++) if(t.hx[i]==t.x[0] && t.hy[i]==t.y[0])                    {                        t.hx[i]=t.x[0]=0;                        t.num--;                    }                    if(t.y[1]-1>=1 && mp[t.x[1]][t.y[1]-1]!='*' && !(t.x[1]==t.x[0] && t.y[1]-1==t.y[0])) t.y[1]--;                    for(i=0;i<2;i++) if(t.hx[i]==t.x[1] && t.hy[i]==t.y[1])                    {                        t.hx[i]=t.x[1]=0;                        t.num--;                    }                    if(!vis[t.x[0]][t.y[0]][t.x[1]][t.y[1]])                    {                        vis[t.x[0]][t.y[0]][t.x[1]][t.y[1]]=1;                        if(t.num==1 && !t.x[0])                        {                            t.x[0]=t.x[1];                            t.y[0]=t.y[1];                            t.x[1]=0;                        }                        t.step++;                        que[bottom++]=t;                    }                }                else                {                    if(t.y[1]-1>=1 && mp[t.x[1]][t.y[1]-1]!='*') t.y[1]--;                    for(i=0;i<2;i++) if(t.hx[i]==t.x[1] && t.hy[i]==t.y[1])                    {                        t.hx[i]=t.x[1]=0;                        t.num--;                    }                    if(t.y[0]-1>=1 &&mp[t.x[0]][t.y[0]-1]!='*' && !(t.x[1]==t.x[0] && t.y[1]==t.y[0]-1)) t.y[0]--;                    for(i=0;i<2;i++) if(t.hx[i]==t.x[0] && t.hy[i]==t.y[0])                    {                        t.hx[i]=t.x[0]=0;                        t.num--;                    }                    if(!vis[t.x[0]][t.y[0]][t.x[1]][t.y[1]])                    {                        vis[t.x[0]][t.y[0]][t.x[1]][t.y[1]]=1;                        if(t.num==1 && !t.x[0])                        {                            t.x[0]=t.x[1];                            t.y[0]=t.y[1];                            t.x[1]=0;                        }                        t.step++;                        que[bottom++]=t;                    }                }//y-1                t=que[top];            }            else//num==1            {                if(t.x[0]+1<=n && mp[t.x[0]+1][t.y[0]]!='*') t.x[0]++;                for(i=0;i<2;i++) if(t.hx[i]==t.x[0] && t.hy[i]==t.y[0])                {                    t.hx[i]=t.x[0]=0;                    t.num--;                }                if(!vis[t.x[0]][t.y[0]][t.x[1]][t.y[1]])                {                    vis[t.x[0]][t.y[0]][t.x[1]][t.y[1]]=1;                    t.step++;                    que[bottom++]=t;                }                t=que[top];                if(t.x[0]-1>=1 && mp[t.x[0]-1][t.y[0]]!='*') t.x[0]--;                for(i=0;i<2;i++) if(t.hx[i]==t.x[0] && t.hy[i]==t.y[0])                {                    t.hx[i]=t.x[0]=0;                    t.num--;                }                if(!vis[t.x[0]][t.y[0]][t.x[1]][t.y[1]])                {                    vis[t.x[0]][t.y[0]][t.x[1]][t.y[1]]=1;                    t.step++;                    que[bottom++]=t;                }                t=que[top];                if(t.y[0]+1<=m && mp[t.x[0]][t.y[0]+1]!='*') t.y[0]++;                for(i=0;i<2;i++) if(t.hx[i]==t.x[0] && t.hy[i]==t.y[0])                {                    t.hx[i]=t.x[0]=0;                    t.num--;                }                if(!vis[t.x[0]][t.y[0]][t.x[1]][t.y[1]])                {                    vis[t.x[0]][t.y[0]][t.x[1]][t.y[1]]=1;                    t.step++;                    que[bottom++]=t;                }                t=que[top];                if(t.y[0]-1>=1 && mp[t.x[0]][t.y[0]-1]!='*') t.y[0]--;                for(i=0;i<2;i++) if(t.hx[i]==t.x[0] && t.hy[i]==t.y[0])                {                    t.hx[i]=t.x[0]=0;                    t.num--;                }                if(!vis[t.x[0]][t.y[0]][t.x[1]][t.y[1]])                {                    vis[t.x[0]][t.y[0]][t.x[1]][t.y[1]]=1;                    t.step++;                    que[bottom++]=t;                }            }            top++;        }        if(top==bottom) printf("Sorry , sir , my poor program fails to get an answer.\n");    }}


54 0