【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 ...
随机推荐
- Jquery inArray的使用
var typeList=["A","B","C","D"]; if ($.inArray("A", ...
- ###Fedora下安装Retext
使用Markdown. #@date: 2012-05-07 #@author: gr #@email: forgerui@gmail.com 因为习惯了Markdown的简单,所以需要在自己的Fed ...
- C语言:Day1~Day4
点击右键查看原图
- Core Animation之CABasicAnimation
在iOS中,图形可分为以下几个层次: 越上层,封装程度越高,动画实现越简洁越简单,但是自由度越低:反之亦然.本文着重介绍Core Animation层的基本动画实现方案. 在iOS中,展示动画可以类比 ...
- C#下如何用NPlot绘制期货股票K线图(1)?
[简介] 作为一名专业程序化交易者,编程是一个程序员的基本功,本文是作者在做的一个期货CTP项目中有关K线绘图的一部分,偿试类MT4中图表 设计而写,在编写绘图时,查阅了相关资料,感觉还是用NPlot ...
- C++ Txt文档写入
void writefile(student *s,int n,string filepath){ ofstream myfile; if(!myfile)//有错误 { exit(1); }else ...
- error RC1205: invalid code page
Get followings error and warnings when building project: error RC1205: invalid code pagewarning C400 ...
- Headfirst设计模式的C++实现——组合模式(Composite)
menu_component.h #ifndef _MENU_COMPONENT_H_ #define _MENU_COMPONENT_H_ #include <string> class ...
- Linux C 程序 基础(FOUR)
1.标识符:C语言本身不限制变量长度,但是某些编译器会限制变量长度,命名最好不要超过8位. 以数字开头,保留字,*,空格非法 2.关键字:类型说明符,int , 语句定义符,if el ...
- read/write数据读写传输方式(转)
前言 笔者本打算撰写一篇讲解标准I/O(缓存I/O)的博文,但是发现已经有网友做过同样的工作,并且工作质量上乘,特转载于此. 原文地址http://lenky.info/archives/2012/0 ...