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 ...
随机推荐
- C语言之形参和实参
一 形参与实参 1).定义 形参:形式参数. 就是定义函数的时候,写在函数名小括号后面的参数叫形参 实参:实际参数. 就是调用函数的时候,调用函数时写在函数名小括号后面的参数就叫实参 2).注意项 a ...
- NSClassFromString,NSSelectorFromString,isKingOfClass
1. NSClassFromString 这个方法判断类是否存在,如果存在就动态加载的,不存为就返回一个空对象; id myObj = [[NSClassFromString(@"MySpe ...
- ubuntu安装jdk的两种方法
方法一: 这种方法比较简单,保证虚拟机网络畅通就可以了 sudo apt-get update sudo apt-get install default-jre sudo apt-get instal ...
- Java 并发 中断线程
Java 并发 中断线程 @author ixenos 对Runnable.run()方法的三种处置情况 1.在Runnable.run()方法的中间中断它 2.等待该方法到达对cancel标志的测试 ...
- linux 学习-用户&群组&权限
Linux用户&群组&权限 ⦁ Linux安全性模型 1)Linux使用User和Group控制使用者对文件的存取权限 2)用户使用账号和口令登录Linux 3) ...
- mybatis学习笔记一(入门)
昨天看了一下mybatis,学习了一下有很多东西还不懂,但是想把一些知道的记录一下,如有错误请大家多多指点. mybatis它是apche的一个开源项目,它以前的没名字并不是叫mybatis而是叫ib ...
- 进度管理工具 planner
ganttproject 太简单,连个子项目都做不了.(也可能是我不会用,后来发现用缩进就可以了.呵呵).又重新有网上搜了一下,发现PLANNER符合我的想法... *进官网,下载. #tar xvJ ...
- Linux scp命令
语法 scp [-1246BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file] [-l limit] [-o ssh_option] [-P p ...
- 【转】python删除文件里包含关键词的行
import shutil with open('/path/to/file', 'r') as f: with open('/path/to/file.new', 'w') as g: for li ...
- QWebView 播放网络视频
最近想看某站的VIP视频,但是网络上的软件用着都不怎么习惯,还有些要收费(收费还不如买VIP了..),所以自己研究做个网络播放器,使用的是QWebView. 1.设置WebView ui->we ...