HDU1026 Ignatius and the Princess I
解题思路:打印路径是关键,细节处理见代码。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = ;
char mapp[maxn][maxn];
int q[maxn*maxn*][]; //队列,刚开始没有乘以4,结果RE了
//dp[i][j]表示走到坐标(i,j)时所用的时间
//pre[x][y]存储当前点的前一个点在队列中的位置
int pre[maxn][maxn], dp[maxn][maxn], n, m, f, r, tmp, t;
int dir[][] = {, , -, , , , , -}; void bfs()
{
f = , r = ; //f为头指针,r为尾指针
q[][] = , q[][] = , dp[][] = ; while(f != r)
{
int x = q[f][], y = q[f][];
tmp = f; //与32行对应
f ++; //出队列 for(int i = ; i < ; i++)
{
int xx = x + dir[i][];
int yy = y + dir[i][];
int tt = dp[x][y] + ;
if(xx < || xx >= n || yy < || yy >= m || mapp[xx][yy] == 'X') continue;
//如果当前点之前到达过,并且所用的时间小于或等于当前点,进行下一次循环。
if(dp[xx][yy] != - && dp[xx][yy] <= tt) continue;
if(mapp[xx][yy] != '.') tt += mapp[xx][yy] - ''; //遇到怪兽,原地扁他
pre[xx][yy] = tmp, q[r][] = xx, q[r][] = yy, dp[xx][yy] = tt, r ++;
}
}
return ;
} int Print(int x,int y)
{
int k = pre[x][y]; if(pre[x][y] == -) return ; //不能走,则返回;
Print(q[k][], q[k][]); //好好体会这里的递归。 printf("%ds:(%d,%d)->(%d,%d)\n", t++, q[k][], q[k][], x, y);
if(mapp[x][y] != '.') //说明是怪兽,原地痛打
for(int i = ; i < mapp[x][y] - ''; i++) //数字多大就打多久
printf("%ds:FIGHT AT (%d,%d)\n", t++, x, y);
return ;
} int main()
{
while(~scanf("%d %d", &n, &m))
{
for(int i = ; i < n; i++)
for(int j = ; j < m; j++) scanf(" %c", &mapp[i][j]); memset(dp, -, sizeof(dp)); //都初始化为没有访问过
memset(pre, -, sizeof(pre));
bfs(); //说明走不到这一点
if(dp[n-][m-] == -) printf("God please help our poor hero.\n");
else
{
printf("It takes %d seconds to reach the target position,"
" let me show you the way.\n", dp[n-][m-]);
t = ;
Print(n - ,m - ); //打印路径。
}
printf("FINISH\n");
}
return ;
}
HDU1026 Ignatius and the Princess I的更多相关文章
- 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 ...
- 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】+【路径记录】
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)
Problem Description The Princess has been abducted by the BEelzebub feng5166, our hero Ignatius has ...
- 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
Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- hdu acm 1028 数字拆分Ignatius and the Princess III
Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- hdu 1029 Ignatius ans the Princess IV
Ignatius and the Princess IV Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32767 K ( ...
随机推荐
- iOS多线程的初步研究(十)-- dispatch同步
GCD提供两种方式支持dispatch队列同步,即dispatch组和信号量. 一.dispatch组(dispatch group) 1. 创建dispatch组 dispatch_group_t ...
- ExtJs之addManagedListener
<!DOCTYPE html> <html> <head> <title>ExtJs</title> <meta http-equiv ...
- Linux网络编程8——对TCP与UDP的简易封装
引言 每次使用socket通信,都会有很对相似的操作.本文,会对TCP与UDP通信做一简单封装,并生成动态库. 代码 my_socket.h #ifndef __MY_SOCKET_H__ #defi ...
- selenium测试框架篇
做自动化框架,不可避免的就是对象库. 有一个好的对象库,可以让整个测试体系: 更容易维护 大大增加代码重用 增加测试系统的稳定性 这里先了解一下我所说的对象库: 所谓的页面对象,是指每一个真是的页面是 ...
- PHP Simple HTML DOM解析器
一直以来使用php解析html文档树都是一个难题.Simple HTML DOM parser 帮我们很好地解决了使用 php html 解析 问题.可以通过这个php类来解析html文档,对其中的h ...
- Java7编程高手进阶读书笔记—集合框架
定义:Java集合框架API是用来表示和操作集合的统一框架,它包含接口.实现类.以及帮助程序员完成一些编程的算法 作用: ●编程更加省力,提高城程序速度和代码质量 ● 非关联的API提高互操作性 ● ...
- mysql和oracle的一个汉字占几个字符
以前一直使用oracle11g,一个汉字占3个字节,所以在操作mysql时也一直这样分配长度. 今天测试了下发现不对了 可以看到第一个的长度确实是15,但是第二个为什么是5? 在网上找到资料:char ...
- 关于MySQL
DBMS - 数据库管理系统(Database Management System) RDBMS - 关系数据库管理系统(Relational Database Management System) ...
- Mac下无法拷贝文件到移动硬盘
Mac下无法拷贝文件到移动硬盘? 是移动硬盘的文件格式的问题. Mac系统无法识别 NTFS 格式的文件. 将移动硬盘格式化为 exFAT 格式的. 别担心,exFAT 格式的硬盘在Windows下也 ...
- AOJ 2170 Marked Ancestor (基础并查集)
http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=45522 给定一棵树的n个节点,每个节点标号在1到n之间,1是树的根节点,有如 ...