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 ( ...
随机推荐
- Eclipse环境下配置spket中ExtJS提示
使用eclipse编写extjs时,一定会用到spket这个插件,spket可以单独当作ide使用,也可以当作eclipse插件使用,我这里是当作eclipse的插件使用的,下面来一步步图解说明如何配 ...
- Lock wait timeout exceeded; try restarting transaction
What gives this away is the word transaction. It is evident by the statement that the query was atte ...
- WCF分布式开发步步为赢(6):WCF服务契约继承与分解设计
上一节我们学习了WCF分布式开发步步为赢(5)服务契约与操作重载部分.今天我们来继续学习WCF服务契约继承和服务分解设计相关的知识点.WCF服务契约继承有何优势和缺点?实际项目里契约设计有什么原则和依 ...
- http://www.cnblogs.com/yjmyzz/p/dubbox-demo.html
http://www.cnblogs.com/yjmyzz/p/dubbox-demo.html
- Linux命令-grep
grep命令用于对文本进行搜索,格式为“grep [选项] [文件]” 搜索某个关键词:"grep 关键词 文本文件" 参数说明 -b 将可执行文件当做文本文件来搜索 -c 仅显示 ...
- 《MySQL悲观锁总结和实践》乐观锁
mysql乐观锁总结和实践 博客分类: MyBatis 数据库 mysql数据库乐观锁悲观锁 上一篇文章<MySQL悲观锁总结和实践>谈到了MySQL悲观锁,但是悲观锁并不是适用于任何场景 ...
- struts2与struts1整合,Unable to load configuration. - interceptor-ref ... struts.xml
struts.xml中为了与struts1的MVC模式整合,需要类似如下的拦截器的引用 <interceptor-stack name="integration"> & ...
- 汇编与C语言混合 实现的从小到大的冒泡排序
汇编实现的从小到大的冒泡排序 主函数由C语言实现,sort函数用汇编语言写 #include <stdio.h> int buffer[256]; //数据缓冲区 int ...
- SQL 随笔
自动生成10位ID DECLARE @num INT ) )) ) Date的运算 DECLARE @StartDate DATETIME , GetDate()) --add day PRINT @ ...
- RPC 原理的前生今世
(如果感觉有帮助,请帮忙点推荐,添加关注,谢谢!你的支持是我不断更新文章的动力.本博客会逐步推出一系列的关于大型网站架构.分布式应用.设计模式.架构模式等方面的系列文章) 在校期间大家都写过不少程序, ...