HDU3309Roll The Cube(BFS)

Roll The Cube

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 484    Accepted Submission(s): 172

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

HDOJ Monthly Contest – 2010.02.06

#include<stdio.h>
#include<iostream>
#include<queue>
#include<string.h>
using namespace std;

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

char tmap[25][25];
int n,m,dir[4][2]={0,1,0,-1,1,0,-1,0},hx[2],hy[2];

int isOk(int x,int y)
{
    if(x<0||x>=n||y<0||y>=m||tmap[x][y]=='*')
        return 0;
    return 1;
}

int bfs(int bx[],int by[])
{
    int time=0,flag=0;
    node p,tp;
    queue<node>q[2];
    //vist1用于两个球相对位置走过的情况,vist2用于剩于一个球与剩于一个洞相对位置走过的情况
    bool vist1[25][25][25][25]={0},vist2[25][25][25][25]={0};

    vist1[bx[0]][by[0]][bx[1]][by[1]]=1;
    vist1[bx[1]][by[1]][bx[0]][by[0]]=1;
    p.x[0]=bx[0]; p.y[0]=by[0];
    p.x[1]=bx[1]; p.y[1]=by[1];
    p.b=2; p.h=2;
    q[0].push(p);

    while(!q[flag].empty())
    {
        time++;
        while(!q[flag].empty())
        {
            p=q[flag].front(); q[flag].pop();

            for(int e=0;e<4;e++)
            {
                tp=p;
                tp.x[0]+=dir[e][0]; tp.y[0]+=dir[e][1];
                tp.x[1]+=dir[e][0]; tp.y[1]+=dir[e][1];

                if(tp.b==2)
                {
                    if(!isOk(tp.x[0],tp.y[0]))  //球0原地不动
                        tp.x[0]=p.x[0],tp.y[0]=p.y[0];
                    if(!isOk(tp.x[1],tp.y[1])) //球1原地不动
                        tp.x[1]=p.x[1],tp.y[1]=p.y[1];
                    if(tp.x[0]==tp.x[1]&&tp.y[0]==tp.y[1]) //两球位置一样
                        continue;
                    if(vist1[tp.x[0]][tp.y[0]][tp.x[1]][tp.y[1]])//两球的相对位置存在过
                        continue;
                    if(tmap[tp.x[0]][tp.y[0]]=='0'||tmap[tp.x[0]][tp.y[0]]=='1')//球0进洞
                    {
                        tp.b=1;  //还剩球1
                        if(tmap[tp.x[0]][tp.y[0]]=='0')
                            tp.h=1; //还剩洞1
                        else tp.h=0; //还剩洞0
                    }
                    if(tmap[tp.x[1]][tp.y[1]]=='0'||tmap[tp.x[1]][tp.y[1]]=='1')//球1进洞
                    {
                        if(tp.b==1)//两球一起进洞
                            return time;

                        tp.b=0; //还剩球0
                        if(tmap[tp.x[1]][tp.y[1]]=='0')
                            tp.h=1;
                        else tp.h=0;
                    }
                    if(tp.b!=2) //只进了一个球
                    {
                        int b,h;
                        b=tp.b;  h=tp.h;
                        vist2[tp.x[b]][tp.y[b]][hx[h]][hy[h]]=1;
                    }

                    vist1[tp.x[0]][tp.y[0]][tp.x[1]][tp.y[1]]=1;
                    vist1[tp.x[1]][tp.y[1]][tp.x[0]][tp.y[0]]=1;
                }
                else
                {
                    int x,y,b,h;
                    b=tp.b;  h=tp.h;
                    x=tp.x[b]; y=tp.y[b];
                    if(!isOk(x,y)||vist2[x][y][hx[h]][hy[h]])
                    continue;
                    if(x==hx[h]&&y==hy[h]) //剩下的球位进入剩下的洞
                        return time;

                    vist2[x][y][hx[h]][hy[h]]=1;
                }
                q[flag^1].push(tp);
            }
        }
        flag^=1;
    }
    return 0;
}

int main()
{
    int T,bk,hk,x[2],y[2];
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        bk=hk=0;
        for(int i=0;i<n;i++)
        {
            scanf("%s",tmap[i]);
            for(int j=0;j<m;j++)
            if(tmap[i][j]=='B')
            {
                x[bk]=i; y[bk]=j; tmap[i][j]='.'; bk++;
            }
            else if(tmap[i][j]=='H')
            {
                hx[hk]=i; hy[hk]=j; tmap[i][j]=hk+'0'; hk++;
            }
        }
        int ans=bfs(x,y);
        if(ans)
            printf("%d\n",ans);
        else
            printf("Sorry , sir , my poor program fails to get an answer.\n");
    }
}
时间: 2024-10-12 07:59:33

HDU3309Roll The Cube(BFS)的相关文章

HDOJ题目3309 Roll The Cube(BFS)

Roll The Cube Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 502    Accepted Submission(s): 181 Problem Description This is a simple game.The goal of the game is to roll two balls to two holes

pots(BFS)

D - Pots Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit Status Description You are given two pots, having the volume of A and B liters respectively. The following operations can be performed: Input On the first and

USACO抓牛catchcow (bfs)

这题是黄巨大出的比赛题. http://poj.org/problem?id=3278 Description Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤

hdu 1728 逃离迷宫 (BFS)

逃离迷宫 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 14376    Accepted Submission(s): 3458 Problem Description 给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置,当然迷宫中有些地方是空地,gloria可以穿越,有些地方

牛汇(BFS)入金具体流程(图文指导)

牛汇开户流程:bfsforex牛汇入金教程 所谓入金,也就是充值的意思,必须充钱到平台才能进行外汇交易.首先,我们先登录bfsforex牛汇官方网站,在交易办公室功能区域下面,点击账户入金: 为您提供中国各大银行的网银支付解决方案,支持人民币支付,和信用卡入金,入金是实时到账的. 牛汇(BFS)入金具体流程(图文指导),布布扣,bubuko.com

URAL 1930 Ivan&#39;s Car(BFS)

Ivan's Car Time limit: 1.5 secondMemory limit: 64 MB The world is in danger! Awful earthquakes are detected all over the world. Houses are destroyed, rivers overflow the banks, it is almost impossible to move from one city to another. Some roads are

【判重+广搜(bfs)】魔板

判重+广搜(bfs)]魔板 Time Limit: 1000MS Memory Limit: 32768KB Special Judge 有一个两行四列的魔板,每个格子里有一个1到8的数字(数字唯一),现在我们可以对魔板进行以下操作: 1.交换两行的数字. 2.将第一列移到第二列,第二列到第三列,第三列到第四列,第四列到第一列. 3.将中间四个数顺时针转一次. 现给你初始状态,我末状态请你用最小的步数将它从初始状态变到末状态. 输入: 前两行,每行4个数表示初状态. 后两行,每行4个数表示末状态

[LeetCode] Binary Tree Zigzag Level Order Traversal(bfs)

Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its zig

USACO Mother&amp;#39;s Milk(bfs)

a=9MvljJDNdls&S=milk3">题目请点我 题解: 水杯倒水的问题非常经典,套路也是一样的,bfs找出全部状态. 这道题的关键在于每次都应该进行六次的倒水尝试,细心一点.PS:三维数组表示状态真的非常方便. 代码实现: /* ID: eashion LANG: C++ TASK: milk3 */ #include <iostream> #include <cstdio> #include <cstdlib> #include &l