hdu1026
#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的更多相关文章
- hdu1026(bfs+优先队列+打印路径)
Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- 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 ...
- 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 ...
- HDU1026 Ignatius and the Princess I
解题思路:打印路径是关键,细节处理见代码. #include<cstdio> #include<cstring> #include<algorithm> using ...
- HDU-1026 Ignatius and the Princess I(BFS) 带路径的广搜
此题需要时间更少,控制时间很要,这个题目要多多看, Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others) Me ...
- hdu1026 Ignatius and the Princess I (优先队列 BFS)
Problem Description The Princess has been abducted by the BEelzebub feng5166, our hero Ignatius has ...
- hdu-1026(bfs+优先队列)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1026 题意:输入n,m和一个n*m的矩阵, .表示通路: x表示墙: n表示有一个怪物,消灭它需要n个 ...
- HDU1026(延时迷宫:BFS+优先队列)
Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- 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 ...
随机推荐
- 如何在网站中加入markdown
在vue组件中加入markdown,模板使用的是webpack 我是这样做的: 因为是npm引入的,所以markdown是遵循CommonJS规范的,需要在webpack.base.conf.js里引 ...
- [ios-必看] iOS 下实现解压缩
http://blog.csdn.net/lyy_whg/article/details/11971581 http://blog.sina.com.cn/s/blog_833996210100udk ...
- [ios2]UItableview相关 【转】
1.使tableview在界面启动后定位在x行 -(void)viewDidLoad { [superviewDidLoad]; NSIndexPath *first = [NSIndex ...
- Fortran中将多个文件进行编译运行的方法
问题:在编译一个单独的Module文件中,发现CentOS7系统中直接使用如下命令无法运行: gfortran program.f90 module.f90 && ./a.out 查找 ...
- 六、spark常见问题总结(转载)
问题导读 1.当前集群的可用资源不能满足应用程序的需求,怎么解决? 2.内存里堆的东西太多了,有什么好办法吗? 1.WARN TaskSchedulerImpl: Initial jo ...
- 有关css3的一些问题
CSS3新特性(阴影.动画.渐变.变形.伪元素等) CSS3与页面布局学习总结--CSS3新特性(阴影.动画.渐变.变形.伪元素等) 目录 一.阴影 1.1.文字阴影 1.2.盒子阴影 二.背 ...
- 瑶瑶GBK好的,UTF-8卡死
请求地址: 开发环境核心 esb : http://10.15.22.120:8866/0203000007/EmpAndDptRelateInfoSync/V1 用gbk可以马上返回. 用utf-8 ...
- 第一百三十四节,JavaScript,封装库--遮罩锁屏
JavaScript,封装库--遮罩锁屏 封装库新增1个方法 /** zhe_zhao_suo_ping()方法,将一个区块元素设置成遮罩锁屏区块 * 注意:一般需要在css文件将元素设置成隐藏 ** ...
- Elasticsearch 5.0 安装 Search Guard 5 插件 (五)
一.Search Guard 简介 Search Guard 是 Elasticsearch 的安全插件.它为后端系统(如LDAP或Kerberos)提供身份验证和授权,并向Elasticsearc ...
- 如何通过fpmmm和zabbix来监控客户机上MariaDB数据库运行情况
首先在客户机安装MariaDB和zabbix,参考上一篇 安装fpmmm的过程主要参考[1]. 安装fpmmm的依赖 shell> yum install php-cli php-process ...