题意:

Ignatius and the Princess I

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 18410    Accepted Submission(s):
5929
Special Judge

http://acm.hdu.edu.cn/showproblem.php?pid=1026
大意 :图大小n*m,从(0,0) 出发到(n-1,m-1),规定'.'为空地,'X'为陷阱,'num'表示要花费num秒与怪物战斗,走一步一秒,求最少秒数方案并打印结果;
因为点上还带了不确定大小的权值,所以应该bfs遍历所有可能,用一个dis数组记录小(0,0)->改点的最小时间,如遇到更短的时间则更改dis的值;
打印路径时使用递归逆序查找,找到的点的dis值加上1再加上当前点打怪的耗时=当前耗时,则使用此点;
代码:

#include<iostream>
#include<cstring>
#include<queue>
#include<cstdio>
using namespace std;
#define inf 0x3f3f3f3f
char e[105][105];
int /*book[105][105],*/dis[105][105];
int n,m,sumn;
int fx[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
int flag;
struct node
{
int x,y,sum;
};
void bfs()
{
queue<node> q;
node next,cur;
node *cu;
cu=&cur;
cu->x=cu->y=cu->sum=0;
//cur.x=cur.y=cur.sum=0;
if (e[0][0]!='.') cu->sum=e[0][0]-'0';
dis[0][0]=cu->sum;
q.push(*cu);
while(!q.empty()){
for (int i=0;i<4;i++){*cu=q.front();
int dx=fx[i][0]+cur.x;
int dy=fx[i][1]+cur.y;
if (dx<0||dy<0||dx>=n||dy>=m||e[dx][dy]=='X') continue;
if (e[dx][dy]=='.') cu->sum+=1;
else cu->sum=cu->sum+1+e[dx][dy]-'0';
if (cu->sum<dis[dx][dy]) dis[dx][dy]=cu->sum;                      //更新此点最小时间值
else continue;
if (dx==n-1&&dy==m-1) {
if(cu->sum<sumn) sumn=cu->sum;                                          //更新到目标点的最小时间
}
cu->x=dx;
cu->y=dy;
q.push(*cu);
}
q.pop();
}
}
void print(int x,int y,int sum)                               //逆序打印
{
int i,j,tim,k,dx,dy;
if (sum==0) return;
if (e[x][y]=='.') tim=0;
else tim=e[x][y]-'0';
for (i=0;i<4;i++){
dx=x+fx[i][0];
dy=y+fx[i][1];
if (dx<0||dy<0||dx>=n||dy>=m||e[dx][dy]=='X') continue;
if (dis[dx][dy]+1+tim==dis[x][y]) {print(dx,dy,sum-1-tim);break;}
}
if (dy>=0) printf("%ds:(%d,%d)->(%d,%d)\n",++flag,dx,dy,x,y);
for (int l=1;l<=tim;l++) printf("%ds:FIGHT AT (%d,%d)\n",++flag,x,y);

}
int main()
{
int i,j;
while (scanf("%d%d",&n,&m)!=EOF){sumn=inf;
flag=0;
memset(dis,inf,sizeof(dis));//getchar();
for(i=0;i<n;i++)
for(j=0;j<m;j++)
scanf(" %c",&e[i][j]);
//cin>>e[i][j];
bfs();
if (sumn==inf) 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",sumn);
print(n-1,m-1,sumn);
}
printf("FINISH\n");
}
return 0;
}

 

h1026 BFS(打印x与路径)的更多相关文章

  1. POJ 3414 Pots ( BFS , 打印路径 )

    题意: 给你两个空瓶子,只有三种操作 一.把一个瓶子灌满 二.把一个瓶子清空 三.把一个瓶子里面的水灌到另一个瓶子里面去(倒满之后要是还存在水那就依然在那个瓶子里面,或者被灌的瓶子有可能没满) 思路: ...

  2. UVA-816.Abbott's Tevenge (BFS + 打印路径)

    本题大意:给定一个迷宫,让你判断是否能从给定的起点到达给定的终点,这里起点需要输入起始方向,迷宫的每个顶点也都有行走限制,每个顶点都有特殊的转向约束...具体看题目便知... 本题思路:保存起点和终点 ...

  3. Codeforces 3A-Shortest path of the king(BFS打印路径)

    A. Shortest path of the king time limit per test 1 second memory limit per test 64 megabytes input s ...

  4. BFS+打印路径

    题目是给你起点sx,和终点gx:牛在起点可以进行下面两个操作: 步行:John花一分钟由任意点X移动到点X-1或点X+1. 瞬移:John花一分钟由任意点X移动到点2*X. 你要输出最短步数及打印路径 ...

  5. HDU 1026(迷宫 BFS+打印)

    题意是要穿过一个迷宫并且将每一步打印出来. 用宽搜的方法找到路径,在 vis 中存一下方向,只是这题被看到的一种不太对的运算符重载坑了很久...... 代码如下: #include <bits/ ...

  6. Light OJ 1429 Assassin`s Creed (II) BFS+缩点+最小路径覆盖

    题目来源:Light OJ 1429 Assassin`s Creed (II) 题意:最少几个人走全然图 能够反复走 有向图 思路:假设是DAG图而且每一个点不能反复走 那么就是裸的最小路径覆盖 如 ...

  7. 迷宫问题 (bfs广度优先搜索记录路径)

    问题描述: 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, ...

  8. BFS和DFS记录路径

    DFS记录路径的意义好像不大,因为不一定是最短的,但是实现起来却很简单. #include<math.h> #include<stdio.h> #include<queu ...

  9. 搜索(BFS)---最短单词路径

    最短单词路径 127. Word Ladder (Medium) Input: beginWord = "hit", endWord = "cog", word ...

随机推荐

  1. C/C++之学习笔记

    [C语言的Static inline 函数的作用]     [printf打印格式] %x  打印十六进制 %d 打印十进制 %b 打印二进制 %c 打印字符 %s 打印字符串 %f 打印单精度flo ...

  2. MySQL索引类型总结和使用技巧

    引用地址:http://www.jb51.net/article/49346.htm 在数据库表中,对字段建立索引可以大大提高查询速度.假如我们创建了一个 mytable表: 复制代码 代码如下: C ...

  3. 架构和性能优化的核心原则(康神sf讲座学习笔记)

    其实架构性能优化的核心就是分,分为分离.分层.分布. 分离动静分离静态资源.动态页面的分离 比如,一个页面有很多静态图片,静态的图片.动态数据.静态CSS.js,图片一般用cdn,但静态资源在使用域名 ...

  4. SIFT在OpenCV中的调用和具体实现(HELU版)

    前面我们对sift算法的流程进行简要研究,那么在OpenCV中,sift是如何被调用的?又是如何被实现出来的了? 特别是到了3.0以后,OpenCV对特征点提取这个方面进行了系统重构,那么整个代码结构 ...

  5. USACO 1.3 Wormholes - 搜索

    Wormholes Farmer John's hobby of conducting high-energy physics experiments on weekends has backfire ...

  6. ubuntu启动google_chrome报错:FATAL:nss_util.cc(632)] NSS_VersionCheck("3.26") failed. NSS >= 3.26 is required. Please upgrade to the latest NSS

    一.背景: jello@jello:~$ lsb_release -aNo LSB modules are available.Distributor ID:    Ubuntu KylinDescr ...

  7. 2018 Machine Learning

    2018/8/13 线性模型(西瓜书P53~P73) Optimizer https://blog.csdn.net/u012151283/article/details/78154917 2018/ ...

  8. EF、Repository、Factory、Service间关系

    EF和Repository 实体(Entities):具备唯一ID,能够被持久化,具备业务逻辑,对应现实世界业务对象. 值对象(Value objects):不具有唯一ID,由对象的属性描述,一般为内 ...

  9. 使用u盘重装双系统中的乌班图

    之前的乌班图被我玩坏了,故而想重装一个.由于之前的双系统是同学帮我装的,我便到网上找各种资料,鼓弄了一天,终于完事了.把过程记录一下. window10 64bit ubuntu 14.04 desk ...

  10. 项目中的一个分页功能pagination

    项目中的一个分页功能pagination <script> //总页数 ; ; //分页总数量 $(function () { // $("#pagination"). ...