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

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

int map[105][105];
int flag[105][105];
int cnt[105][105];//记录怪的血量
int to[4][2] = {1,0,-1,0,0,1,0,-1};
int n,m,tim;

int check(int x,int y)
{
if(x<0 || y<0 || x>=n || y>=m)
return 1;
if(map[x][y] == -1)
return 1;
return 0;
}

int bfs()
{
int i;
priority_queue<node> Q;
node a,next;
a.x = 0;
a.y = 0;
a.step = 0;
map[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++)
{
next = a;
next.x+=to[i][0];
next.y+=to[i][1];
if(check(next.x,next.y))
continue;
next.step =a.step+map[next.x][next.y]+1;//记录时间
map[next.x][next.y] = -1;//标记为走过
flag[next.x][next.y] = i+1;//记录朝向
Q.push(next);
}
}
return 0;
}

void print(int x,int y)
{
int n_x,n_y;
if(!flag[x][y])
return;
n_x = x - to[flag[x][y]-1][0];
n_y = y - to[flag[x][y]-1][1];
print(n_x,n_y);
printf("%ds:(%d,%d)->(%d,%d)\n",tim++,n_x,n_y,x,y);
while(cnt[x][y]--)
{
printf("%ds:FIGHT AT (%d,%d)\n",tim++,x,y);
}
}

int main()
{
int i,j;
while(~scanf("%d%d",&n,&m))
{
memset(map,0,sizeof(map));
memset(flag,0,sizeof(flag));
memset(cnt,0,sizeof(cnt));
char s[105];
for(i = 0; i<n; i++)
{
scanf("%s",s);
for(j = 0; s[j]; j++)
{
if(s[j] == '.')
map[i][j] = 0;
else if(s[j] == 'X')
map[i][j] = -1;
else
map[i][j] = cnt[i][j] = s[j]-'0';
}
}
int ans = 0;
ans = bfs();
if(ans)
{
printf("It takes %d seconds to reach the target position, let me show you the way.\n",ans);
tim = 1;
print(n-1,m-1);
}
else
printf("God please help our poor hero.\n");
printf("FINISH\n");
}

return 0;
}

hdu1026的更多相关文章

  1. hdu1026(bfs+优先队列+打印路径)

    Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  2. hdu1026.Ignatius and the Princess I(bfs + 优先队列)

    Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  3. hdu---------(1026)Ignatius and the Princess I(bfs+dfs)

    Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  4. HDU1026 Ignatius and the Princess I

    解题思路:打印路径是关键,细节处理见代码. #include<cstdio> #include<cstring> #include<algorithm> using ...

  5. HDU-1026 Ignatius and the Princess I(BFS) 带路径的广搜

      此题需要时间更少,控制时间很要,这个题目要多多看, Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Me ...

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

    Problem Description The Princess has been abducted by the BEelzebub feng5166, our hero Ignatius has ...

  7. hdu-1026(bfs+优先队列)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1026 题意:输入n,m和一个n*m的矩阵, .表示通路: x表示墙: n表示有一个怪物,消灭它需要n个 ...

  8. HDU1026(延时迷宫:BFS+优先队列)

    Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  9. HDU1026 Ignatius and the Princess I 【BFS】+【路径记录】

    Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

随机推荐

  1. 在VS中配置并测试opencv

    什么是opencv? opencv是一个计算机视觉库.它目前有两个分支2.4.X和3.X,2.4.X版本是经典版本,网上的教程资源大多是关于它的,所以推荐使用2.4.X. 它能帮我做什么? 这意味着你 ...

  2. [开发者账号] ios7苹果开发者账号申请

    1.登陆  https://developer.apple.com/ 2.点击网站最底部的 3.点击 4.然后根据提示继续点击 注意:1.点击的过程中注意个人和企业账号  开发者类型等 2.付款账号的 ...

  3. JS算法之快排&冒泡

    1.快速排序思想: 1.1 先找数组的最中间的一个数为基准 1.2 把数组通过此基准分为小于基准的left数组和大于基准的right数组, 1.3 递归重复上面的两个步骤, 代码如下: functio ...

  4. RFC Transactional RFC (tRFC) queue RFC(qRFC) 概念

    Transactional RFC When using transactional RFC (tRFC), the called function module is executed exactl ...

  5. PostgreSQL Replication之扩展与BDR

    在这一章中,将向您介绍一个全新的技术,成为BDR.双向复制(BDR),在PostgreSQL的世界里,它绝对是一颗冉冉升起的新星.在不久的将来,许多新的东西将会被看到,并且人们可以期待一个蓬勃发展的项 ...

  6. CentOS7安装和配置Nginx(https)

    安装Nginx下载安装包# wget http://nginx.org/download/nginx-1.11.7.tar.gz# tar -zxvf nginx-1.11.7.tar.gz# cd ...

  7. Python 自动给数字前面补0

    为了排版方便或者是输出文件命名整洁,通常需要给数字前面补0来做统一.Python中有一个zfill函数用来给字符串前面补0,非常有用,这个zfill看起来也就是zero fill的缩写吧,看一下如何使 ...

  8. COCOS2D-JS入门-web端项目部署

    下载cocos2d-js文件,建议上官网下载(外国官网或者中国官网都可以) 外国官网:http://cocos2d-x.org/download(选择最新版即可,我下载时为3.9版本,大概300多M) ...

  9. HTTP 错误 403.14 - Forbidden的解决办法

    错误: HTTP 错误 403.14 - ForbiddenWeb 服务器被配置为不列出此目录的内容.   原因: 出现这个错误,是因为默认文档中没有增加index.aspx导致的. 解决方法: 打开 ...

  10. sulime text3

    sublime text 3 详细说明--包括快捷键 sublime 插件安装 快捷键 sunlime (需要先安装package control,ctrl+shift+p,输入insall之后安装插 ...