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 ( ...
随机推荐
- PATH环境变量和CLASSPATH环境变量详解
大凡装过JDK的人都知道要安装完成后要设置环境变量,可是为什么要设置环境变量呢?环境变量有什么作用? 1)PATH详解: 计算机安装JDK之后,输入“javac”“java”之类的命令是不能马上被计算 ...
- Python之socketserver源码分析
一.socketserver简介 socketserver是一个创建服务器的框架,封装了许多功能用来处理来自客户端的请求,简化了自己写服务端代码.比如说对于基本的套接字服务器(socket-based ...
- 李洪强iOS开发之OC[008] -创建一个对象并访问实例变量
// // main.m // 07 - 创建一个对象并且访问实例变量 // // Created by vic fan on 16/7/3. // Copyright © 2016年 李洪强 ...
- lintcode:二叉树的路径和
题目 给定一个二叉树,找出所有路径中各节点相加总和等于给定 目标值 的路径. 一个有效的路径,指的是从根节点到叶节点的路径. 解题 下面有个小bug 最后比较的时候是叶子节点为空,左右都有叶子结点,所 ...
- 欧拉工程第56题:Powerful digit sum
题目链接 Java程序 package projecteuler51to60; import java.math.BigInteger; import java.util.Iterator; im ...
- Android 核心分析之十三Android GWES之Android窗口管理
Android GWES之Android窗口管理1基本构架原理 Android的窗口管理是C/S模式的.Android中的Window是表示Top Level等顶级窗口的概念.DecorView是Wi ...
- Spring框架学习之第6节
bean的生命周期 为什么总是一个生命当做一个重点? Servlet –> servlet生命周期 Java对象生命周期 往往笔试,面试总喜欢问生命周期的问题? ① 实例化(当我们的程序加载 ...
- Android:Android SDK Manager
Android SDK Manager 包含:Tools(构建工具.编译工具.平台工具等) .各种版本SDK.Extras(安卓知识库和辅助工具) 每个SDK至少包含:1.SDK Plaform 2. ...
- [iOS]为什么不要在init初始化方法里调用self.view
首先.如果你调用self.view的时候,就会调用view的getter方法, 这个时候,view是空的,那么系统就会自动给你创建一个view,然后就会触发ViewDidLoad方法.那么这个时候,如 ...
- [vim]设置vim语法高亮显示和自动缩进
1.配置文件的位置 在目录 /etc/vim下面,有个名为vimrc的文件,这是系统中公共的vim配置文件,对所有用户都有效.而在每个用户的主目录下,都可以自己建立私有的配置文件,命名为 ...