HDU3309-Roll The Cube

28 篇文章 0 订阅

Roll The Cube

                                                                          Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
                                                                                                       Total Submission(s): 702    Accepted Submission(s): 283


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
  
  
4 6 3 *** *B* *B* *H* *H* *** 4 4 **** *BB* *HH* **** 4 4 **** *BH* *HB* **** 5 6 ****** *.BB** *.H*H* *..*.* ******
 

Sample Output
  
  
3 1 2 Sorry , sir , my poor program fails to get an answer.
 

Author
MadFroG
 

Source


#include <stdio.h>
#include <string.h>
#include <queue>
#include <algorithm>

using namespace std;

struct node
{
    int x[2],y[2],step;
    int b[2],h[2];
}s;

char map[25][25];
bool vis[25][25][25][25];
int sx[2],sy[2],n,m;
int dir[4][2] = {{0,1},{1,0},{0,-1},{-1,0}};

int bfs()
{
    queue<node> q;
    node pre,next;
    s.x[0]=sx[0],s.y[0]=sy[0];
    s.x[1]=sx[1],s.y[1]=sy[1];
    s.step=0;
    s.b[0]=s.b[1]=s.h[0]=s.h[1]=0;
    q.push(s);
    memset(vis,false,sizeof(vis));
    while(!q.empty())
    {
        pre=q.front();
        q.pop();
        for(int i=0; i<4; i++)
        {
            next=pre;
            for(int j=0; j<2; j++)
            {
                if(next.b[j]) continue;
                next.x[j]+=dir[i][0];
                next.y[j]+=dir[i][1];
                if(map[next.x[j]][next.y[j]]=='*')
                {
                    next.x[j]-=dir[i][0];
                    next.y[j]-=dir[i][1];
                }
            }
            if(vis[next.x[0]][next.y[0]][next.x[1]][next.y[1]]) continue;
            if(next.x[0]==next.x[1]&&next.y[0]==next.y[1]&&next.b[0]+next.b[1]==0) continue;
            next.step++;
            vis[next.x[0]][next.y[0]][next.x[1]][next.y[1]]=true;
            int flag=1;
            for(int j=0; j<2; j++)
            {
                int k=map[next.x[j]][next.y[j]];
                if(k<2&&!next.h[k])
                {
                    next.h[k]=1;
                    next.b[j]=1;
                }
                if(!next.b[j])
                    flag=0;
            }
            if(flag) return next.step;
            q.push(next);
        }
    }
    return -1;
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d %d",&n,&m);
        int cnt=0,len=0;
        for(int i =0; i<n; i++)
        {
            scanf("%s",map[i]);
            for(int j=0; j<m; j++)
            {
                if(map[i][j]=='H') map[i][j]=cnt++;
                else if(map[i][j]=='B')
                {
                    sx[len]=i,sy[len]=j;
                    len++;
                }
            }
        }
        int ans=bfs();
        if(ans!=-1) printf("%d\n",ans);
        else printf("Sorry , sir , my poor program fails to get an answer.\n");
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值