Hoj 2979 Escape from Pyramids

本题练习广搜:

题目链接:http://acm.hit.edu.cn/hoj/problem/view?id=2979

本题十分简单,典型的FloodFIll。输出格式细节地方需要注意即可。

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

int map[202][202];

int dis[202][202];

struct Point
{
    int x;
    int y;
};

char c;

int disx[6] = {-1,0,1,1,0,-1};
int disy[6] = {1,2,1,-1,-2,-1};

int main()
{
#ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
#endif
    int h,t;
    int temp;
    int curx,cury;
    int num = 0;
    int flag = 0;
    int ans;
    while(scanf("%d %d",&h,&t) == 2)
    {
        num++;
        if(num!=1)
        {
            printf("\n");
        }
        flag = 0;
        memset(map,0,sizeof(map));
        memset(dis,0,sizeof(dis));
        for(int i=0;i<h;i++)
        {
           for(int j=0;j<i+1;j++)
           {
               scanf(" %c",&c);
               if(c == 'S')
               {
                   temp = 1;
                   curx = i;
                   cury = h-i-1+j*2;
               }
               else if(c == '@')
               {
                   temp = 2;
               }
               else if(c == '*')
               {
                   temp = 3;
               }
               else if(c == 'D')
               {
                   temp = 4;
               }
               map[i][h-i-1+j*2] = temp;
           }
        }
        queue<Point> p;
        Point nex,temp;
        nex.x = curx;
        nex.y = cury;
        p.push(nex);
        while(!p.empty())
        {
            temp = p.front();
            p.pop();
            for(int i=0;i<6;i++)
            {
                int tempx = temp.x + disx[i];
                int tempy = temp.y + disy[i];
                if(tempx>=0 && tempx<h && tempy>=0 && tempy<2*h)
                {
                    if(map[tempx][tempy] == 2)
                    {
                        dis[tempx][tempy] = dis[temp.x][temp.y] + 1;
                        nex.x = tempx;
                        nex.y = tempy;
                        p.push(nex);
                        map[tempx][tempy] = 5;//标志为已访问过
                    }
                    else if(map[tempx][tempy] == 4)
                    {
                        flag = 1;
                        ans = dis[temp.x][temp.y] + 1;
                        printf("Maze #%d :\n",num);
                        if(ans<=t)
                        {
                            if(ans == 1)
                            {
                                printf("Hurry up, You need %d minute to escape!\n",ans);
                            }
                            else
                            {
                                printf("Hurry up, You need %d minutes to escape!\n",ans);
                            }
                        }
                        else
                        {
                            printf("Oh No, I'm afraid that you don't have enough time to escape!\n");
                        }
                        break;
                    }
                }
            }
            if(flag == 1)
            {
                break;
            }
        }
        if(flag == 0)
        {
            printf("Maze #%d :\n",num);
            printf("Oh No, I'm afraid that you don't have enough time to escape!\n");
        }
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值