HDU3309:Roll The Cube(BFS)

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.

并没有太复杂,相信都能看懂了

#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];
};
char map[25][25];
bool vis[25][25][25][25];
int sx[2],sy[2],n,m;
int to[4][2] = {0,1,1,0,0,-1,-1,0};

int bfs()
{
    queue<node> Q;
    node a,next;
    a.x[0] = sx[0],a.y[0]=sy[0];
    a.x[1] = sx[1],a.y[1]=sy[1];
    a.step = 0;
    a.b[0] = a.b[1] = a.h[0] = a.h[1] = 0;
    Q.push(a);
    memset(vis,false,sizeof(vis));
    while(!Q.empty())
    {
        a = Q.front();
        Q.pop();
        int i,j;
        for(i = 0; i<4; i++)
        {
            next = a;
            for(j = 0; j<2; j++)
            {
                if(next.b[j]) continue;
                next.x[j]+=to[i][0];
                next.y[j]+=to[i][1];
                if(map[next.x[j]][next.y[j]]=='*')
                {
                    next.x[j]-=to[i][0];
                    next.y[j]-=to[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(j = 0; j<2; j++)
            {
                int now = map[next.x[j]][next.y[j]];
                if(now<2 && !next.h[now])
                {
                    next.h[now]=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,i,j;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        int cnt = 0,len = 0;
        for(i = 0; i<n; i++)
        {
            scanf("%s",map[i]);
            for(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;
}
时间: 2024-11-05 18:42:00

HDU3309:Roll 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

HDU-3309-Roll The Cube(BFS)

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

HDU 4801 Pocket Cube BFS

链接:http://vjudge.net/contest/view.action?cid=51289#problem/K 题意:给一个小魔方(每面只有四个小块),每个小块有自己的颜色(一共六种颜色,每种颜色四块),问经过最多N次操作(N<=7),每次操作是将魔方某一面转动90度,最多能使多少个面的四个小块颜色都相同. 思路:每次操作产生6种状态,(向前,向后,向左,向右,向上,向下,左半魔方向前和右半魔方向后的操作产生的状态本质是一样的)直接BFS搜索,可能产生的状态一共有6^7种,中间比较麻烦

【HDOJ】3309 Roll The Cube

BFS,考虑一球进洞仅一球滚动以及两球重叠的情况即可. 1 /* 3309 */ 2 #include <iostream> 3 #include <queue> 4 #include <cstdio> 5 #include <cstring> 6 #include <cstdlib> 7 using namespace std; 8 9 #define MAXN 25 10 11 typedef struct { 12 int x[2], y[

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

wewe

BFS广搜题目有时间一个个做下来 2009-12-29 15:09 1574人阅读 评论(1) 收藏 举报 图形graphc优化存储游戏 有时间要去做做这些题目,所以从他人空间copy过来了,谢谢那位大虾啦. pku 1175 Starry Night 题目地址:http://acm.pku.edu.cn/JudgeOnline/problem?id=1175 解法:BFS,要注意的是如何判断图形是一样的,我的做法就是计算每两个点的距离之和. 看:http://hi.baidu.com/doxi

2013山东省赛Rubik’s cube 魔方BFS

二阶魔方,只有0,1 问最少多少步可以转成每个面都为0,或1 BFS即可,对应好旋转时候的关系,因为顺时针转三次和逆时针转1次的效果一样,所以只要6种旋转方式即可,判重可用map省空间,或者直接判省时间 #include "stdio.h" #include "string.h" #include "map" #include "queue" using namespace std; bool mp[17000000]; st

BFS POJ3322 Bloxorz I

Bloxorz I Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6108   Accepted: 2007 Description Little Tom loves playing games. One day he downloads a little computer game called 'Bloxorz' which makes him excited. It's a game about rolling a

POJ-3131-Cubic Eight-Puzzle(双向BFS+哈希)

Description Let's play a puzzle using eight cubes placed on a 3 × 3 board leaving one empty square. Faces of cubes are painted with three colors. As a puzzle step, you can roll one of the cubes to a adjacent empty square. Your goal is to make the spe