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 ( ...
随机推荐
- Content Providers详解
今天仔细阅读了一遍Content Providers的官方API文档,总结了一下Android中Content Providers的用法. 各种类型的Content Provider对一个结构化的数据 ...
- iOS多线程的初步研究(二)-- 锁
谈到线程同步,一般指如何对线程间共享数据的同步读写,如何避免混乱的读写结果.一个基本的解决办法就是使用锁(LOCK). iOS提供多种同步锁的类和方法,这里介绍下基本用法. 1. NSLock:最基本 ...
- Linux网络编程8——对TCP与UDP的简易封装
引言 每次使用socket通信,都会有很对相似的操作.本文,会对TCP与UDP通信做一简单封装,并生成动态库. 代码 my_socket.h #ifndef __MY_SOCKET_H__ #defi ...
- CentOS 配置vncserver
一.安装 以root用户运行以下命令来安装vncserver; yum install tigervnc-server 同样运行以下命令来安装vncviewer; yum install vnc 停止 ...
- linux下使用crontab定时备份MYSQL数据库的方法:
摘要 linux下使用crontab定时备份MYSQL数据库的方法: 只需按照下面3步做,一切都在你的掌控之下: 第一步:在服务器上配置备份目录代码: ------------------------ ...
- Passing JavaScript Objects to Managed Code
Silverlight If the target managed property or input parameter is strongly typed (that is, not typed ...
- Eclipse 下如何删除一个项目的 SVN 信息
选中项目,右键 - Team - 断开连接 出现如下对话框,根据需要,选择 “删除”或者“不删除”,点击 Yes 即可
- Hibernate —— ID的各种生成器(转)
Hibernate中,<id>元素下的可选<generator>子元素是一个Java类的名字,用来为该持久化类的实例生成惟一标示,所有的生成器都实现net.sf.hiberna ...
- How to install cacti With Nginx
转载于:https://github.com/blackyboy/Ubuntu-Linux-Stuffs/blob/master/How-to-install-Cacti-Monitoring-Ser ...
- Getting Error "Invalid Argument to LOCATOR.CONTROL: ORG_LOCATOR_CONTROL='' in Material Requirements Form (文档 ID 1072379.1)
APPLIES TO: Oracle Work in Process - Version 11.5.10.2 and later Information in this document applie ...