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 (Java/Others)
Total Submission(s): 10006 Accepted Submission(s): 3004
Special Judge
1.Ignatius can only move in four directions(up, down, left, right), one step per second. A step is defined as follow: if current position is (x,y), after a step, Ignatius can only stand on (x-1,y), (x+1,y), (x,y-1) or (x,y+1).
2.The array is marked with some characters and numbers. We define them like this:
. : The place where Ignatius can walk on.
X : The place is a trap, Ignatius should not walk on it.
n : Here is a monster with n HP(1<=n<=9), if Ignatius walk on it, it takes him n seconds to kill the monster.
Your task is to give out the path which costs minimum seconds for Ignatius to reach target position. You may assume that the start position and the target position will never be a trap, and there will never be a monster at the start position.
.XX.1.
..X.2.
2...X.
...XX.
XXXXX.
5 6
.XX.1.
..X.2.
2...X.
...XX.
XXXXX1
5 6
.XX...
..XX1.
2...X.
...XX.
XXXXX.
1s:(0,0)->(1,0)
2s:(1,0)->(1,1)
3s:(1,1)->(2,1)
4s:(2,1)->(2,2)
5s:(2,2)->(2,3)
6s:(2,3)->(1,3)
7s:(1,3)->(1,4)
8s:FIGHT AT (1,4)
9s:FIGHT AT (1,4)
10s:(1,4)->(1,5)
11s:(1,5)->(2,5)
12s:(2,5)->(3,5)
13s:(3,5)->(4,5)
FINISH
It takes 14 seconds to reach the target position, let me show you the way.
1s:(0,0)->(1,0)
2s:(1,0)->(1,1)
3s:(1,1)->(2,1)
4s:(2,1)->(2,2)
5s:(2,2)->(2,3)
6s:(2,3)->(1,3)
7s:(1,3)->(1,4)
8s:FIGHT AT (1,4)
9s:FIGHT AT (1,4)
10s:(1,4)->(1,5)
11s:(1,5)->(2,5)
12s:(2,5)->(3,5)
13s:(3,5)->(4,5)
14s:FIGHT AT (4,5)
FINISH
God please help our poor hero.
FINISH
题意:
给你一个 N*M 的图, 要你从第一个点走到最后一个点【从左上角走到右下角】
只可以按照上下左右四个方向走
. : 代表可以走
X : 表示是墙,不可以走
n : 代表这里有一个怪兽,打败怪兽用时 n
每走一步耗时 1 .
如果能到达,则输出最小时间和每一步的走法
不能到达输出。。。
具体输出看样例。
注意:保证起点没有怪兽,终点不是墙。【也就是说终点可能有怪兽】
算法:优先队列+BFS 【本质Dijkstra】
思路:
//定义优先队列:对于入队了的点,先出队的是时间少的,那么第一个到达终点的就是结果
struct Node{
int x,y; //当前到达的点
int time; //耗费的时间 bool operator < (const Node &b) const{
return b.time < time;
}
};
关于优先队列的重载一直不是很清楚。。。
code:
#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
#include<iostream>
using namespace std; const int maxn = 110;
const int INF = maxn*maxn*10; int map[maxn][maxn]; //记录图
int vis[maxn][maxn]; //标记入队
char str[maxn];
int n,m; int dir[4][2] = {0,1, 0,-1, -1,0, 1,0}; //定义优先队列:对于入队了的点,先出队的是时间少的,那么第一个到达终点的就是结果
struct Node{
int x,y; //当前到达的点
int time; //耗费的时间 bool operator < (const Node &b) const{
return b.time < time;
}
}; //每一个点的前驱, 由于是逆向搜索的, 所以记录的其实是当前点的下一个点了
struct Pre{
int px, py;
}pre[maxn][maxn]; void bfs()
{
Node now, next;
priority_queue<Node> q;
while(!q.empty()) q.pop(); now.x = n; now.y = m; //从终点走向起点
now.time = map[n][m]; //注意:终点也可能会有怪兽
pre[n][m].px = -1; //输出边界
q.push(now); memset(vis, 0, sizeof(vis)); //为方便快速输出路径, 从终点往起点找
vis[n][m] = 1; //标记终点入队 while(!q.empty())
{
now = q.top(); q.pop(); if(now.x == 1 && now.y == 1) //一旦到达起点
{
printf("It takes %d seconds to reach the target position, let me show you the way.\n", now.time);
int time = 1;
int x = now.x, y = now.y; //当前的位置
int nx = pre[x][y].px, ny = pre[x][y].py; //下一个位置
while(pre[x][y].px != -1) //不停的找前驱
{
printf("%ds:(%d,%d)->(%d,%d)\n", time++, x-1, y-1, nx-1, ny-1);
while(map[nx][ny]--) //如果有怪兽
{
printf("%ds:FIGHT AT (%d,%d)\n", time++, nx-1, ny-1);
}
x = nx; y = ny; //继续查找下一个点
nx = pre[x][y].px, ny = pre[x][y].py;
}
printf("FINISH\n");
return; //结束
} for(int i = 0; i < 4; i++)
{
next.x = now.x+dir[i][0];
next.y = now.y+dir[i][1]; if(map[next.x][next.y] >= 0 && !vis[next.x][next.y]) //当前点可以走,并且没有入队过
{
vis[next.x][next.y] = 1; //标记入队 next.time = now.time + 1 + map[next.x][next.y];
pre[next.x][next.y].px = now.x; //前驱记录
pre[next.x][next.y].py = now.y; q.push(next);
}
}
} printf("God please help our poor hero.\n"); //不能到达
printf("FINISH\n");
return;
} int main()
{
while(scanf("%d%d", &n,&m) != EOF)
{
gets(str);
for(int i = 0; i <= n+1; i++) //周围加边
for(int j = 0; j <= m+1; j++)
map[i][j] = -1; char c;
for(int i = 1; i <= n; i++) //输出的时候注意 -1 处理下
{
for(int j = 1; j <= m; j++)
{
scanf("%c", &c);
if(c != 'X')
{
if(c == '.') map[i][j] = 0;
else map[i][j] = c-'0';
}
}
gets(str);
} bfs();
}
return 0;
}
hdu 1026 Ignatius and the Princess I【优先队列+BFS】的更多相关文章
- hdu 1026 Ignatius and the Princess I(优先队列+bfs+记录路径)
以前写的题了,现在想整理一下,就挂出来了. 题意比较明确,给一张n*m的地图,从左上角(0, 0)走到右下角(n-1, m-1). 'X'为墙,'.'为路,数字为怪物.墙不能走,路花1s经过,怪物需要 ...
- HDU 1026 Ignatius and the Princess I(BFS+优先队列)
Ignatius and the Princess I Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d &am ...
- 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 (J ...
- HDU 1026 Ignatius and the Princess I (BFS)
题目链接 题意 : 从(0,0)点走到(N-1,M-1)点,问最少时间. 思路 : BFS..... #include <stdio.h> #include <string.h> ...
- hdu 1026 Ignatius and the Princess I(BFS+优先队列)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1026 Ignatius and the Princess I Time Limit: 2000/100 ...
- hdu 1026 Ignatius and the Princess I
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1026 Ignatius and the Princess I Description The Prin ...
- hdu 1026:Ignatius and the Princess I(优先队列 + bfs广搜。ps:广搜AC,深搜超时,求助攻!)
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+记录路径)(priority_queue)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1026 Problem Description The Princess has been abducted ...
- hdu 1026 Ignatius and the Princess I 搜索,输出路径
Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
随机推荐
- Spring Boot Logback应用日志
e Spring Boot Logback应用日志 2015-09-08 19:57 7673人阅读 评论(0) 收藏 举报 . 分类: Spring Boot(51) . 目录(?)[+] 日志对于 ...
- 数据结构- 串的模式匹配算法:BF和 KMP算法
数据结构- 串的模式匹配算法:BF和 KMP算法 Brute-Force算法的思想 1.BF(Brute-Force)算法 Brute-Force算法的基本思想是: 1) 从目标串s 的第一个字 ...
- 【ActiveMQ】持久化消息队列的三种方式
1.ActiveMQ消息持久化方式,分别是:文件.mysql数据库.oracle数据库 2.修改方式: a.文件持久化: ActiveMQ默认的消息保存方式,一般如果没有修改过其他持久化方式的话可以不 ...
- iOS 滤镜 转载,原文见正文首行链接
转载自:http://blog.sina.com.cn/s/blog_5fb39f9101018gv7.html 直接上代码了: // // ViewController.m // 图片模糊处理 // ...
- java concurrency: ThreadLocal及其实现机制
转载:http://shmilyaw-hotmail-com.iteye.com/blog/1703382 ThreadLocal概念 从字面上来理解ThreadLocal,感觉就是相当于线程本地的. ...
- strcpy_s
char src[5]="abcd"; char *des=new char[str.length(src)+1]; // length()不计\0 strcpy_s(des, ...
- mysql 表及其列字符集设置
--修改表的字符集 alter table rtb_media_daily_report character set gbk; --查询表列字符集 show full columns from rtb ...
- Fedora最小化安装后没有ifconfig命令
百度了很久,都说添加export PATH=$PATH:/sbin,结果我用whereis 查询了,根本没有这个命令.后来直接yum search ifconfig,查找到原来是net-tools包. ...
- ASP.NET MVC3中的路由系统 Routes
MVC中,用户访问的地址并不映射到服务器中对应的文件,而是映射到对应Control里对应的ActionMethod,由ActionMethod来决定返回用户什么样的信息.而把用户访问的地址对应到对应的 ...
- vs2010根据字符串内容添加断点
在vs中我们可以直接用表达式.数值型比较直接用操作符即可. 如i==2,i<2; 但是字符型比较呢? 加入我们有一个名为string的变量,定义如下: char *string="Tw ...