HDU 1026 BSF+优先队列+记录路径、
#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+优先队列+记录路径、的更多相关文章
- hdu 1226 BFS + bfs记录路径
http://acm.hdu.edu.cn/showproblem.php? pid=1226 为了节省空间.您可以使用vis初始化数组初始化-1. 发现BFSeasy错了地方 始一直WA在这里:就是 ...
- hdu 1026 Ignatius and the Princess I(优先队列+bfs+记录路径)
以前写的题了,现在想整理一下,就挂出来了. 题意比较明确,给一张n*m的地图,从左上角(0, 0)走到右下角(n-1, m-1). 'X'为墙,'.'为路,数字为怪物.墙不能走,路花1s经过,怪物需要 ...
- 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 ...
- hdu 1026(优先队列+路径输出)
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)
http://acm.hdu.edu.cn/showproblem.php?pid=1026 题意:给出一个迷宫,求出到终点的最短时间路径. 这道题目在迷宫上有怪物,不同HP的怪物会损耗不同的时间,这 ...
- 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 ...
- 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 1074(状态压缩dp+记录路径)
题意:给了n个家庭作业,然后给了每个家庭作业的完成期限和花费的实践,如果完成时间超过了期限,那么就要扣除分数,然后让你找出一个最优方案使扣除的分数最少,当存在多种方案时,输出字典序最小的那种,因为题意 ...
- hdu 1664(数论+同余搜索+记录路径)
Different Digits Time Limit: 10000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
随机推荐
- ES 6.X的环境搭建
一.ES windows环境下的安装 1.官网下载windows安装包 2.解压启动 F:\software\ES\elasticsearch-6.4.0\bin\elasticsearch 3.查看 ...
- CISC和RISC是什么?它们的特点和区别?
CISC的英文全称为“Complex Instruction Set Computer”,即“复杂指令系统计算机”,从计算机诞生以来,人们一直沿用CISC指令集方式.早期的桌面软件是按CISC设计的, ...
- 下载额外数据文件失败 以下软件包要求安装后下载附加数据,但其数据无法下载或无法处理 ttf-mscorefonts-installer
故障显示: 一些软件包的数据文件无法下载 以下软件包要求安装后下载附加数据,但其数据无法下载或无法处理. ttf-mscorefonts-installer 这是一个永久错误,系统中的这些软件包将无法 ...
- MVVMDemo
QueryCommand.cs using System;using System.Collections.Generic;using System.Linq;using System.Text;us ...
- golang 命令行参数
package main import ( "fmt" "flag" ) func main() { //定义几个变量,用于接收命令行的参数值 var user ...
- Facebook iOS App如何优化启动时间
http://www.cocoachina.com/ios/20160105/14870.html 提高 Facebook 应用的性能已经成为 Facebook 持续关注的领域.因为我们相信一个高性能 ...
- 下载安装APK(兼容Android7.0)
我们使用手机的时候经常会看到应用程序提示升级,大部分应用内部都需要实现升级提醒和应用程序文件(APK文件)下载. 一般写法都差不多,比如在启动app的时候,通过api接口获得服务器最新的版本号,然后和 ...
- 【记录Bug】 This is probably not a problem with npm. There is likely additional logging output above.
一个eslint的错误 我的报错如下 $ npm install > node-sass@4.11.0 install C:\Users\Administrator\Desktop\forGit ...
- ubuntu 使用glfw.h 出现函数无法调用
最近在学习在Ubuntu下使用qt进行opengl开发,使用到了glfw这个库.我安装官网的编译和安装方法进行了配置安装,在usr/local/include的下产生了glfw.h文件. 于是我在我的 ...
- AbstractExecutorService的submit方法概要介绍
1.概述 ExecutorService是JDK提供的框架,它简化了异步模式下的任务执行.一般来说,ExecutorService会自动提供一个线程池和API,用于为其分配任务. 2.实例化Execu ...