#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<stack>
using namespace std;
const int qq=110;
const int MAX=10000;
char map[qq][qq];
int pre[qq][qq];
int a[MAX],b[MAX];
int n,m;
int dir[5][2]={0,0,1,0,-1,0,0,1,0,-1}; // 走的顺序是右,左,下,上
struct Node
{
int x,y;
int pre; // 定义来源的方向 1左 2右 3上 4下
char mark; //是否存在怪兽
int time;
friend bool operator <(Node a,Node b)
{
return a.time>b.time;
}
};
priority_queue<Node>Q;
int check(int x,int y)
{
if(x<0||y<0||x>=n||y>=m) //判断是否越界
return 0;
if(pre[x][y]) // 判断是否走过这点
return 0;
if(map[x][y]=='X') // 判断是否是墙
return 0;
return 1;
}
void bfs()
{
Node ans,cns;
ans.x=0;ans.y=0;ans.pre=0;ans.time=0;ans.mark=map[0][0];
Q.push(ans);
while(!Q.empty()){
cns=Q.top();
Q.pop();
//printf("%d %d\n",cns.x,cns.y);
if(cns.x==n-1&&cns.y==m-1){
//printf("%d %d\n",cns.x,cns.y);
while(!Q.empty())
Q.pop();
Q.push(cns);
break;
}
for(int i=1;i<=4;++i){
ans.x=cns.x+dir[i][1];
ans.y=cns.y+dir[i][0];
if(check(ans.x,ans.y)){
ans.mark=map[ans.x][ans.y];
ans.pre=i;
pre[ans.x][ans.y]=i;
if(map[ans.x][ans.y]>='1'&&map[ans.x][ans.y]<='9')
ans.time=cns.time+1+(map[ans.x][ans.y]-'0');
else
ans.time=cns.time+1;
Q.push(ans);
}
}
}
return;
}
void out()
{
Node ans;
ans=Q.top();
Q.pop();
printf("It takes %d seconds to reach the target position, let me show you the way.\n",ans.time);
int r=1;
a[0]=ans.x;b[0]=ans.y;
int x=ans.x,y=ans.y;
//printf("%d %d\n",x,y);
while(x!=0||y!=0){ // x和y搞反了 调了两个小时、 真特么醉了
if(pre[x][y]==1)
y=y-1;
else if(pre[x][y]==2)
y=y+1;
else if(pre[x][y]==3)
x=x-1;
else if(pre[x][y]==4)
x=x+1;
a[r]=x;b[r++]=y;
// printf("%d %d\n",x,y);
}
int t=1;
for(int i=r-2;i>=0;--i){
printf("%ds:(%d,%d)->(%d,%d)\n",t++,a[i+1],b[i+1],a[i],b[i]);
if(map[a[i]][b[i]]>='1'&&map[a[i]][b[i]]<='9')
for(int j=0;j<map[a[i]][b[i]]-'0';++j)
printf("%ds:FIGHT AT (%d,%d)\n",t++,a[i],b[i]);
}
}
int main()
{
while(~scanf("%d %d",&n,&m)){
memset(pre,0,sizeof(pre));
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
for(int i=0;i<n;++i)
scanf("%s",map[i]);
bfs();
if(Q.empty()) printf("God please help our poor hero.\n");
else out();
printf("FINISH\n");
while(!Q.empty())
Q.pop();
}
return 0;
}
// 记录路径的数组开小了、又调了好久!!QAQ

HDU 1026 BSF+优先队列+记录路径、的更多相关文章

  1. hdu 1226 BFS + bfs记录路径

    http://acm.hdu.edu.cn/showproblem.php? pid=1226 为了节省空间.您可以使用vis初始化数组初始化-1. 发现BFSeasy错了地方 始一直WA在这里:就是 ...

  2. hdu 1026 Ignatius and the Princess I(优先队列+bfs+记录路径)

    以前写的题了,现在想整理一下,就挂出来了. 题意比较明确,给一张n*m的地图,从左上角(0, 0)走到右下角(n-1, m-1). 'X'为墙,'.'为路,数字为怪物.墙不能走,路花1s经过,怪物需要 ...

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

  4. hdu 1026(优先队列+路径输出)

    Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  5. HDU 1026 Ignatius and the Princess I(带路径的BFS)

    http://acm.hdu.edu.cn/showproblem.php?pid=1026 题意:给出一个迷宫,求出到终点的最短时间路径. 这道题目在迷宫上有怪物,不同HP的怪物会损耗不同的时间,这 ...

  6. hdu 1026 Ignatius and the Princess I 搜索,输出路径

    Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

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

  8. hdu 1074(状态压缩dp+记录路径)

    题意:给了n个家庭作业,然后给了每个家庭作业的完成期限和花费的实践,如果完成时间超过了期限,那么就要扣除分数,然后让你找出一个最优方案使扣除的分数最少,当存在多种方案时,输出字典序最小的那种,因为题意 ...

  9. hdu 1664(数论+同余搜索+记录路径)

    Different Digits Time Limit: 10000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

随机推荐

  1. Django项目:CRM(客户关系管理系统)--04--02PerfectCRM创建ADMIN页面02

    十.CRM项目创建模板页面 {#king_base.html#} {## ————————02PerfectCRM创建ADMIN页面————————#} {#模板文件 king_base.html#} ...

  2. visual studio code 调试reactjs

    1.首先到visual studio code官网下载ide. 2.打开visual studio code,点击右侧菜单条的小图标 找到[Debugger for Chrome],并安装 3.打开c ...

  3. html5之本地数据库

    <!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8" ...

  4. ASP.NET 自定义服务器控件

    文章内容   本文通过创建一个最简单的服务器控件,演示开发服务器端控件的流程. 文章内容整理自MSDN的编程指南,原文地址在文章末尾的资源中. 本文创建一个简单的服务器控件,名为 RedLabel.  ...

  5. Excel按照某一列的重复数据设置隔行变颜色效果

    问题:如图所示,想按照A列中的重复数据设置隔重复行变颜色的效果,能否通过条件格式命令实现. 方法1:(最佳答案) 条件格式公式:=MOD(SUMPRODUCT(--($A$1:$A1<>$ ...

  6. 关于父组件通过v-on接收子组件多个参数的一点研究

    写组件的时候遇到一个需求,我需要在子组件向父组件传递信息 this.$emit('myEvent', 信息1, 信息2) 在父组件使用v-on来接收 <my-component @myEvent ...

  7. js中字符串的加密base64

    base64编码主要用在传输,存储表示二进制的领域,还可以进行加密和解密.其实就是字符串的编码和解码 btoa与atob 只能加密ascii,不能加密汉字. var str = 'I LOVE YOU ...

  8. Mysql+php报错原因

    SQL syntax --语法错误,看near,错误会在near后引号中的内容 的附近 Table/Database....... dosen't existes ---表/库(库名/表名) 不存在 ...

  9. Directx11教程(47) alpha blend(4)-雾的实现

    原文:Directx11教程(47) alpha blend(4)-雾的实现      除了用来实现透明效果之外,我们还可以用alpha blend来实现雾(fog)的效果.通过逐渐清晰的雾气效果,可 ...

  10. Libevent:7Bufferevents基本概念

    很多时候,应用程序除了能响应事件之外,还希望能够处理一定量的数据缓存.比如,当写数据的时候,一般会经历下列步骤: l  决定向一个链接中写入一些数据:将数据放入缓冲区中: l  等待该链接变得可写: ...