关闭

HDU 1026 Ignatius and the Princess I BFS优先队列

95人阅读 评论(0) 收藏 举报
分类:

    一看题目的输入输出这题的意思基本也就明白了......迷宫问题,肯定又要写麻烦的遍历了= =

    不过题意还是要简单说下,'X'是墙,'.'是路,迷宫的标准╮(╯_╰)╭起点是左上角,终点是右下角,数字是在这一点上要多费的时间,问最短时间走到终点的方法。一开始用dfs写没写出来= =好吧,写的乱乱的最后也没保留,最后又上网看了下改的bfs才搜出来最短路,然后再输出路径......好像也没什么可多说的了= =放代码吧......

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
char s[105];
int mg[105][105];
int vis[105][105];
int hp[105][105];
int change[4][2]={1,0,-1,0,0,1,0,-1};
int n,m,tim;

struct node
{
    int x,y,step;
    friend bool operator<(node n1,node n2)
    {
        return n2.step<n1.step;
    }
};

int bfs()
{
    int i;
    node a,b;
    priority_queue<node> q;
    a.x=0;
    a.y=0;
    a.step=0;
    mg[0][0]=-1;
    q.push(a);
    while(!q.empty())
    {
        a=q.top();
        q.pop();
        if(a.x==n-1&&a.y==m-1)
            return a.step;
        for(i=0;i<4;i++)
        {
            b=a;
            b.x=b.x+change[i][0];
            b.y=b.y+change[i][1];
            if(b.x<0||b.y<0||b.x>=n||b.y>=m)
                continue;
            if(mg[b.x][b.y]==-1)
                continue;
            b.step=a.step+mg[b.x][b.y]+1;
            mg[b.x][b.y]=-1;
            vis[b.x][b.y]=i+1;
            q.push(b);
        }
    }
    return 0;
}

int print(int x,int y)
{
    int tx,ty;
    if(!vis[x][y])
        return 0;
    tx=x-change[vis[x][y]-1][0];
    ty=y-change[vis[x][y]-1][1];
    print(tx,ty);
    cout<<tim<<"s:("<<tx<<","<<ty<<")->("<<x<<","<<y<<")"<<endl;
    tim++;
    while(hp[x][y]--)
    {
        cout<<tim<<"s:FIGHT AT ("<<x<<","<<y<<")"<<endl;
        tim++;
    }
    return 0;
}

int main()
{
    int i,j;
    int ans;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        memset(mg,0,sizeof(mg));
        memset(vis,0,sizeof(vis));
        memset(hp,0,sizeof(hp));
        memset(s,0,sizeof(s));
        for(i=0;i<n;i++)
        {
            scanf("%s",s);
            for(j=0;s[j];j++)
            {
                if(s[j]=='.')
                {
                    mg[i][j]=0;
                }
                else if(s[j]=='X')
                {
                    mg[i][j]=-1;
                }
                else
                {
                    mg[i][j]=s[j]-'0';
                    hp[i][j]=s[j]-'0';
                }
            }
        }
        ans=0;
        ans=bfs();
        if(ans==0)
        {
            cout<<"God please help our poor hero."<<endl;
        }
        else
        {
            cout<<"It takes "<<ans<<" seconds to reach the target position, let me show you the way."<<endl;
            tim=1;
            print(n-1,m-1);
        }
        cout<<"FINISH"<<endl;
    }
    return 0;
}


1
0
查看评论
发表评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场

杭电 hdu 1026 Ignatius and the Princess I(BFS+优先队列+墨迹人的输出)

Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (...
  • mengxiang000000
  • mengxiang000000
  • 2016-01-19 12:23
  • 1084

HDU--杭电--1026--Ignatius and the Princess I--广搜--直接暴力0MS,优先队列的一边站

别人都是广搜+优先队列,我没空临时学,所以就直接自己暴力了 Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)  ...
  • hncu1206401liuhao
  • hncu1206401liuhao
  • 2013-08-03 14:04
  • 1993

BFS广度优先搜索——入门

BFS——广度优先搜索 广度优先搜索是通过对图的完全遍历来达到要求的点的算法。其对图的遍历是如同波浪一样,每层按照制定的方式一层一层向下搜。 如: 5 5 4 2 ...
  • ilblue
  • ilblue
  • 2016-10-26 21:03
  • 1753

hdu 1026 Ignatius and the Princess I bfs 优先队列 路径记录

Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (...
  • linmushi
  • linmushi
  • 2015-09-18 15:51
  • 259

hdu1026 Ignatius and the Princess I (优先队列 BFS)

Problem Description The Princess has been abducted by the BEelzebub feng5166, our hero Ignatius has...
  • u010372095
  • u010372095
  • 2013-08-02 20:18
  • 1150

HDU 1026 Ignatius and the Princess I(BFS+优先队列)

Problem Description The Princess has been abducted by the BEelzebub feng5166, our hero Ignatius h...
  • tomorrowtodie
  • tomorrowtodie
  • 2016-02-27 05:04
  • 302

HDU 1026 Ignatius and the Princess I bfs优先队列

Ignatius and the Princess I Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (J...
  • xky140610205
  • xky140610205
  • 2016-07-16 09:23
  • 220

Hdu 1026 Ignatius and the Princess I (BFS 优先队列+路径输出)

给一个 N*M 的四方向连通的图, 要从左上角走到右下角,. : 代表可以走,X :  表示是墙,不可以走,n :  代表这里有一个怪兽,打败怪兽用时 n,每走一步耗时 1 .如果能到达,则输出最小时...
  • whyorwhnt
  • whyorwhnt
  • 2014-01-20 09:39
  • 701

【搜索之BFS + 优先队列】杭电 hdu 1026 Ignatius and the Princess I

/* THE PROGRAM IS MADE BY PYY */ /*-----------------------------------------------------------------...
  • panyanyany
  • panyanyany
  • 2012-04-02 12:15
  • 916

HDU 1026 Ignatius and the Princess I 【BFS(优先队列)+路径存储】

Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (...
  • u013806814
  • u013806814
  • 2014-11-19 00:53
  • 481
    个人资料
    • 访问:33315次
    • 积分:1730
    • 等级:
    • 排名:千里之外
    • 原创:130篇
    • 转载:5篇
    • 译文:0篇
    • 评论:10条
    文章分类
    QQ二维码(添加请写明备注)
    最新评论