这道题搞了很久啊。搜索非常好的一道题。昨天想了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的更多相关文章

  1. 【HDOJ】1027 Ignatius and the Princess II

    这道题目最开始完全不懂,后来百度了一下,原来是字典序.而且还是组合数学里的东西.看字典序的算法看了半天才搞清楚,自己仔细想了想,确实也是那么回事儿.对于长度为n的数组a,算法如下:(1)从右向左扫描, ...

  2. 【HDOJ】1098 Ignatius's puzzle

    数学归纳法,得证只需求得使18+ka被64整除的a.且a不超过65. #include <stdio.h> int main() { int i, j, k; while (scanf(& ...

  3. 【母函数】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; ...

  4. hdu 1026 Ignatius and the Princess I

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1026 Ignatius and the Princess I Description The Prin ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. HDU 1026 Ignatius and the Princess I(BFS+优先队列)

    Ignatius and the Princess I Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d &am ...

随机推荐

  1. UIButton关于setFont方法过时的解决方法

    环境:xcode7 语言:Object-c 解决方法:更改UIButton的titleLabel属性的font值 一.新建一个Single View Application项目 二.创建一个UIBut ...

  2. OC_NSString

    // // main.m // OC_NSString // // Created by qianfeng on 15/6/10. // Copyright (c) 2015年 qianfeng. A ...

  3. NodeJS连接MongoDB数据库时报错

    今天第一次尝试连接MongoDB数据库,具体步骤也很简单. 首先,通过NodeJS运行环境安装MongoDB包,进入要安装的目录,执行语句 npm install mongodb 安装成功后,通过如下 ...

  4. Google 编码风格

    一.Google JavaScript编码风格 简体中文版 Google JavaScript Style Guide 二.Google HTML/CSS代码风格指南 简体中文版 三.Google C ...

  5. OpenJudge 2739 计算对数

    1.链接地址: http://bailian.openjudge.cn/practice/2739/ 2.题目: 总时间限制: 1000ms 内存限制: 65536kB 描述 给定两个正整数a和b.可 ...

  6. HDOJ(1005) Number Sequence

    这道题,咋一看很像Fibonacci数列,使用递归或者改进的动态规划来解决.但是仔细一看,(1 <= n <= 100,000,000),使用这些方法,要么超时,要么肯定内存不够用,因此必 ...

  7. mysql 之权限介绍

    转自:http://tech.it168.com/a2010/0114/837/000000837456_all.shtml 一.MySQL授权表概述首先从全局开始,如果全局的是允许的,即在 user ...

  8. Javascript常见全局函数

      ØdecodeURI() 解码某个编码的 URI ØencodeURI() 把字符串编码为 URI ØdecodeURIComponent() 解码一个编码的 URI 组件 ØencodeURIC ...

  9. 使用CSS完成元素居中的七种方法

    在网页布局中元素水平居中比元素垂直居中要简单不少,同时实现水平居中和垂直居中往往是最难的.现在是响应式设计的时代,我们很难确切的知道元素的准确高度和宽度,所以一些方案不大适用.据我所知, 在CSS中至 ...

  10. Django初体验

    为什么使用Django 快速开发 使用python 数据库ORM系统 大量内置应用 后台管理系统 admin 用户认证系统 auth 会话系统 sessions 安全性高 表单验证 SQL注入 跨站点 ...