程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> HDU3309:Roll The Cube(BFS)

HDU3309:Roll The Cube(BFS)

編輯:C++入門知識

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 
#include 
#include 
#include 
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 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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved