[BFS]HOJ2979Escape from Pyramids

传送门:Escape from Pyramids

Escape from Pyramids

My Tags  (Edit)
  Source : Lambyy
  Time limit : 1 sec   Memory limit : 64 M

Submitted : 435, Accepted : 113

Description

A pyramid is a structure where the outer surfaces are triangular and converge at a point. The base of a pyramid can be trilateral, quadrilateral, or any polygon shape, meaning that a pyramid has at least three triangular surfaces (at least four faces including the base). The square pyramid, with square base and four triangular outer surfaces, is a common version.

The most famous pyramids are the Egyptian pyramids — huge structures built of brick or stone, some of which are among the world's largest constructions.

Several days ago, lambyy toke part in a tourist party with some close friends. When they went on holiday in Egypt, they found a strange pyramid that consist of hexagon blocks, but the shape of the pyramid still can be seen as triangle, just as the following picture Fig 1 shows.

They were very curious of this pyramid and they went there happily. But out of their expectation, the pyramid itself is a complex maze, so they got stuck. Also, as the pyramid is antient, some blocks are turn into barriers. What's worse, the hazy sky was turning dusk, if they could not go out of the pyramid before dark, they will be dangerous! Can they survive at all?

To simplify the problem, here you may see the pymarid as a plane, with every block of which being a hexagon. And, except the border, every block have six adjacent positions. If we define each part have a coordinate (x, y) as Fig 2, you may know that position (2, 1) have six adjacent positions (1, 0), (1, 1), (2, 0), (2, 2), (3, 1), (3, 2). The blue block (0, 0) is the position that they stand, the green block (2, 0) is the exit position, and the red blocks (1, 0) (2, 1) are barriers which cannot be stepped into. Here, as the block is quite huge, the time to go from one block to its adjacent blocks is one minute. So, as the yellow arrow shows in Fig 2, they need at least five minutes to survive by the route (0, 0) -> (1, 1) -> (2, 2) -> (3, 2) -> (3, 1) -> (2, 0).

Now they give you the map, and the left time the sky would get dark, can you help them calculate whether they can survive?

Input

There are many test cases. For each test case, there are two numbers h and t in the first line (2 ≤ h ≤ 100, 0 ≤ t ≤ 500), h mean the size of pyramid (here, you may consider Fig 1 is a 4-size pyramid), and after t minutes, the sky will get dark.

Then in the following h lines, there comes the map of the pyramid. There are four kinds of letters in the map:

  • 'S', mean the current position that lambyy and his friends stand, there are one and only one 'S' in one map.
  • 'D', mean the exit position of the pyramid, there are at least one 'D' in one map.
  • '@', mean the block can be stepped into.
  • '*', mean the block is a barrier that cannot be stepped into.

Output

In the first line of each test case, output "Maze # c:" .

If they cannot survive, output "Oh No, I'm afraid that you don't have enough time to escape!";

If they can survive, please output "Hurry up, You need k minute(s) to escape!", here k is the least time they need to go out of the pyramid. You may notice that if they only need one minute, you should output "1 minute" here, and if they need more, output "k minutes". If you don't understand, please see the Sample Output.

Output a blank line between two test cases.

Sample Input

4 5
   S
  * @
 D * @
@ @ @ @
4 5
   S
  * @
 D * @
@ * @ @
2 2
 S
D D

Sample Output

Maze #1 :
Hurry up, You need 5 minutes to escape!

Maze #2 :
Oh No, I'm afraid that you don't have enough time to escape!

Maze #3 :
Hurry up, You need 1 minute to escape!


解题报告:

此题依旧是BFS,因为BFS可以找到一条最短的路,代码如下:

#include<iostream>
#include<queue>
#include<stack>
#include<cstdio>
using namespace std;
struct node{
   int x,y,l;
}st;
int flag;
char s[110][110];
int dx[6]={-1,-1, 0, 0, 1, 1};
int dy[6]={ 0,-1, 1,-1, 0, 1};
void bfs(node st,int h,int t){
    queue<node> q;
    int i,ans;
    node a,b;
    while(!q.empty()){
        q.pop();
    }
    q.push(st);
    st.l=0;
    ans=-1;
    s[st.x][st.y]='*';
    while(!q.empty()){
        a=q.front();
        q.pop();
        if(s[a.x][a.y]=='D'){
            ans=a.l;
            break;
        }
        for(i=0;i<6;i++){
            b.x=a.x+dx[i];
            b.y=a.y+dy[i];
            if(!(b.x<=h&&b.y<=b.x&&b.x>=1&&b.y>=1))
                continue;
            if(s[b.x][b.y]!='*'){
                b.l=a.l+1;
                if(s[b.x][b.y]!='D')
                    s[b.x][b.y]='*';
                q.push(b);
            }
        }
    }
    if(flag)
        printf("\n");
    printf("Maze #%d :\n",++flag);
    if(ans>=0&&ans<=t){
        if(ans > 1)
            printf("Hurry up, You need %d minutes to escape!\n",ans);
        else
            printf("Hurry up, You need 1 minute to escape!\n");
    }
    else
        printf("Oh No, I'm afraid that you don't have enough time to escape!\n");
}
int main(){
    int i,j,h,t;
    flag=0;
    while(scanf("%d%d",&h,&t)==2){
        for(i=1;i<=h;i++){
            for(j=1;j<=i;j++){
                scanf(" %c",&s[i][j]);
                if(s[i][j]=='S'){
                    st.x=i;
                    st.y=j;
                }
            }
        }
        bfs(st,h,t);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值