【HDOJ】1026 Ignatius and the Princess I
这道题搞了很久啊。搜索非常好的一道题。昨天想了2小时,以为是深搜,但后来发现深搜怎么也没法输出正确路径。今天拿宽搜试了一下,问题就是普通的队列宽搜没法得到当前时间最小值。看了一下讨论区,发现优先级队列。好久不用了,都忘记了。各种忘记,优先级队列排序都忘掉了。搞了好半天。最后还需要注意的是格式化输出,采用栈格式输出。需要保存每个节点的移动方向。并且注意若终点是怪兽,还是需要"Fight"。这道题目感觉不是一道水题,还挺不错。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
using namespace std; #define MAXNUM 105
#define isDigit(ch) ch>='0' && ch<='9' typedef struct node_st{
int x, y, time;
node_st() { }
node_st(int a, int b, int t) {
x = a; y = b; time = t;
}
friend bool operator < (node_st p, node_st q) {
return p.time > q.time;
}
} node_st; priority_queue<node_st> que; typedef struct subnode {
int x, y;
subnode() { }
subnode (int a, int b) {
x = a; y = b;
}
} subnode; stack<subnode> path;
char map[MAXNUM][MAXNUM];
bool visit[MAXNUM][MAXNUM];
int pos[MAXNUM][MAXNUM];
int direct[][]={{,-}, {-,}, {,}, {,}};
int n, m; void output(); bool check(int x, int y) {
if (x>= && x<n && y>= && y<m) {
if (visit[x][y]==false && map[x][y]!='X')
return true;
else
return false;
} else
return false;
} void bfs() {
node_st cur, next;
int newx, newy, newt; que.push(node_st(,,));
visit[][] = true;
while ( !que.empty() ) {
cur = que.top();
que.pop();
// printf("cur node: x=%d,y=%d,cur.time=%d\n",cur.x, cur.y, cur.time);
if (cur.x==n- && cur.y==m-) {
printf("It takes %d seconds to reach the target position, let me show you the way.\n", cur.time);
output();
printf("FINISH\n");
return ;
}
for (int i=; i<; ++i) {
newx = cur.x + direct[i][];
newy = cur.y + direct[i][];
newt = cur.time + ;
if ( check(newx, newy) ) {
next = node_st(newx, newy, newt);
if ( isDigit(map[newx][newy]) )
next.time += map[newx][newy] - '';
// printf("after check, ch=%c, time=%d\n", map[newx][newy], next.time);
pos[newx][newy] = i;
visit[newx][newy] = true;
que.push(next);
}
}
}
printf("God please help our poor hero.\n");
printf("FINISH\n");
} int main() {
int i; while (scanf("%d %d", &n, &m) != EOF) {
getchar();
for (i=; i<n; ++i) {
gets(map[i]);
}
memset(visit, false, sizeof(visit));
memset(pos, , sizeof(pos));
while (!que.empty())
que.pop();
while (!path.empty())
path.pop();
bfs();
} return ;
} void output() {
subnode cur, next;
int x, y, i, time = ; x = n-; y =m-;
while (x||y) {
path.push(subnode(x, y));
i = pos[x][y];
x -= direct[i][];
y -= direct[i][];
}
// Don;t forget to push (0, 0)
path.push(subnode(, )); cur = path.top();
path.pop(); while (!path.empty()) {
next = path.top(); x = cur.x;
y = cur.y;
if (map[x][y] != '.') {
i = map[x][y] - '';
while (i--) {
printf("%ds:FIGHT AT (%d,%d)\n", ++time, x, y);
}
}
time++;
printf("%ds:(%d,%d)->(%d,%d)\n", time, cur.x, cur.y, next.x, next.y); cur = path.top();
path.pop();
}
// if final-point has a monster, then ight
x = next.x;
y = next.y;
if (map[x][y] != '.') {
i = map[x][y] - '';
while (i--) {
printf("%ds:FIGHT AT (%d,%d)\n", ++time, x, y);
}
}
}
【HDOJ】1026 Ignatius and the Princess I的更多相关文章
- 【HDOJ】1027 Ignatius and the Princess II
这道题目最开始完全不懂,后来百度了一下,原来是字典序.而且还是组合数学里的东西.看字典序的算法看了半天才搞清楚,自己仔细想了想,确实也是那么回事儿.对于长度为n的数组a,算法如下:(1)从右向左扫描, ...
- 【HDOJ】1098 Ignatius's puzzle
数学归纳法,得证只需求得使18+ka被64整除的a.且a不超过65. #include <stdio.h> int main() { int i, j, k; while (scanf(& ...
- 【母函数】hdu1028 Ignatius and the Princess III
大意是给你1个整数n,问你能拆成多少种正整数组合.比如4有5种: 4 = 4; 4 = 3 + 1; 4 = 2 + 2; 4 = 2 + 1 + 1; 4 = 1 + 1 + 1 + 1; ...
- 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+优先队列)
传送门: 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【优先队列+BFS】
链接: http://acm.hdu.edu.cn/showproblem.php?pid=1026 http://acm.hust.edu.cn/vjudge/contest/view.action ...
- hdoj 1026 Ignatius and the Princess I 最小步数,并且保存路径
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(BFS+优先队列)
Ignatius and the Princess I Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d &am ...
随机推荐
- ###Git使用问题
#@date: 2014-05-04 #@author: gerui #@email: forgerui@gmail.com 一.git reset的使用 今天修改了代码,就git add ./,添加 ...
- JavaScript学习笔记(3)——JavaScript与HTML的组合方式
一.JavaScript可以写在HTML页面内部, 可位于 HTML 的 <body> 或 <head> 部分中,或者同时存在于两个部分中. 通常的做法是把函数放入 <h ...
- android 权限总结
1.拨打电话要权限 2.sd目录存东西要权限
- SerializableMaplist传递数据
package com.xjx.data; import java.io.Serializable; import java.util.List; import java.util.Map; /** ...
- EF 的 霸气配置
通过EF 作为操作数据库的工具有一段时间了,也做了几个相对不大的项目,慢慢的也对EF的使用摸索出来了一些规则,虽然说不是技术难点,但是,我说的是但是,能够提高我们开发效率的棉花糖有时我们还是必须要吃的 ...
- NodeJS+ExpressJS+SocketIO+MongoDB应用模板
OS:Win8.1 with update 关键字:NodeJS,ExpressJS,SocketIO,MongoDB. 1.源代码下载:https://github.com/ldlchina/ESM ...
- 两个不同于LR和jmeter的性能测试工具
LR图形界面,更利于使用 jmeter采用java,也能够扩展 相对于上两款工具,下面两款性能测试工具都采用了异步IO模型,扩展性都更强速度也更快 gatling:基于scala,速度相比更快性能压力 ...
- C#表驱动法+一点反射实现“得到指定位数随机不重复字符串”三种方式的封装
1.结构 第一个类 public class GetMethods{...} 类中的变量: ...
- log4j配置文件写法
### direct log messages to stdout ###log4j.rootLogger=DEBUG,stdoutlog4j.appender.stdout=org.apache.l ...
- 7.MVC框架开发(创建层级项目)
在一个项目比较大的时候,就会有多个层级项目 1)在项目中选定项目右建新建区域(新的层级项目),项目->右键->添加->区域,构成了一套独立的MVC的目录,这个目录包括Views,Co ...