#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. MyEclipse导入Maven项目pom文件第一行报错,运行Tomcat报Log4j错误--解决方法

    问题描述: 前一段时间电脑第一次导入Maven项目,又是pom文件错,改好后又是运行Tomcat报Log4j错误,一直倒腾了近一个月程序才成功跑起来,太不容易. 也上网查了很长时间,没一个方法能解决我 ...

  2. 编译 MVC View

    默认MVC的 View页面 不参与编译,当更改view对应model后,view编译也能通过,或者页面有错误的服务端代码时也不会报错. 那么如何在编译的时候能让View中的错误也不能通过呢.经过查找找 ...

  3. [CSS] 子元素垂直居中的两种方式

    1. 多个子元素水平并排,IE10以下失效 display: flex; align-items: center; justify-content: center; 2.多个子元素竖直排列,这种方式会 ...

  4. Windows设置VMware开机自动启动,虚拟机也启动

    很多用windows系统电脑开发的童鞋,会在自己电脑上装一个虚拟机,然后在装一个linux系统当作服务器来使用.但每次电脑开机都要去重启一下虚拟机电源,实在是不划算.下面博主教大家在windows系统 ...

  5. Santa Claus and a Palindrome

    Santa Claus and a Palindrome 题目链接:http://codeforces.com/contest/752/problem/D 贪心 很自然地,可以想到,若subS不是回文 ...

  6. mysql数据恢复问题

    现象 mysql> drop database zabbix; Query OK, 104 rows affected (0.30 sec)mysql> exitBye[root@mysq ...

  7. 距离VR时代的真正到来还有多久?

    2016年被称为是VR元年,各大VR设备商的宣传攻势铺天盖地,众VR产品看的人眼花缭乱.随着平民化进程不断推进以及渗透率的提升,VR成为近两年来最引人关注的焦点,在众多领域的共同作用下,VR时代是否即 ...

  8. JS 用角度换东南西北

    最近因为业务,正好需要用设备回传的角度值转成用户读得懂的文字形式 function toDirStr(num){ var num=parseInt(num) var N='北'; var E='东'; ...

  9. jQuery DOM 元素方法 - index() 方法

    元素的 index,相对于选择器 获得元素相对于选择器的 index 位置. 该元素可以通过 DOM 元素或 jQuery 选择器来指定. 语法 $(selector).index(element) ...

  10. win8 or win7安装ubuntu双系统

    安装双系统的效果 现在使用win和linux双系统,整个环境相当方便好用,比如在Linux系统上,仍能访问NTFS(win的文件系统格式)中的文件和文档,当然win下的一些像matlab.vs等是不能 ...