h1026 BFS(打印x与路径)
题意:
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
#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与路径)的更多相关文章
- POJ 3414 Pots ( BFS , 打印路径 )
题意: 给你两个空瓶子,只有三种操作 一.把一个瓶子灌满 二.把一个瓶子清空 三.把一个瓶子里面的水灌到另一个瓶子里面去(倒满之后要是还存在水那就依然在那个瓶子里面,或者被灌的瓶子有可能没满) 思路: ...
- UVA-816.Abbott's Tevenge (BFS + 打印路径)
本题大意:给定一个迷宫,让你判断是否能从给定的起点到达给定的终点,这里起点需要输入起始方向,迷宫的每个顶点也都有行走限制,每个顶点都有特殊的转向约束...具体看题目便知... 本题思路:保存起点和终点 ...
- 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 ...
- BFS+打印路径
题目是给你起点sx,和终点gx:牛在起点可以进行下面两个操作: 步行:John花一分钟由任意点X移动到点X-1或点X+1. 瞬移:John花一分钟由任意点X移动到点2*X. 你要输出最短步数及打印路径 ...
- HDU 1026(迷宫 BFS+打印)
题意是要穿过一个迷宫并且将每一步打印出来. 用宽搜的方法找到路径,在 vis 中存一下方向,只是这题被看到的一种不太对的运算符重载坑了很久...... 代码如下: #include <bits/ ...
- Light OJ 1429 Assassin`s Creed (II) BFS+缩点+最小路径覆盖
题目来源:Light OJ 1429 Assassin`s Creed (II) 题意:最少几个人走全然图 能够反复走 有向图 思路:假设是DAG图而且每一个点不能反复走 那么就是裸的最小路径覆盖 如 ...
- 迷宫问题 (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, ...
- BFS和DFS记录路径
DFS记录路径的意义好像不大,因为不一定是最短的,但是实现起来却很简单. #include<math.h> #include<stdio.h> #include<queu ...
- 搜索(BFS)---最短单词路径
最短单词路径 127. Word Ladder (Medium) Input: beginWord = "hit", endWord = "cog", word ...
随机推荐
- 在uboot中加入cmd_run命令,运行环境变量
在学习uboot的过程中会经常烧录程序,每次都要敲一些下载指令.这样是不是很麻烦,有什么办法能快速的烧写呢.很简单,将需要敲击的指令编译到uboot中,以环境变量的形式存在.但是环境变量很好加,如何运 ...
- 关于mysql连接抛出10038错误问题
今天用Navicat Premium连接windows server 2003 mysql的时候, 抛出10038问题, 这种问题之前在rhel也出现过一次, 就是防火墙不允许连接kill掉了这个请求 ...
- JAVA I/O(六)多路复用IO
在前边介绍Socket和ServerSocket连接交互的过程中,读写都是阻塞的.套接字写数据时,数据先写入操作系统的缓存中,形成TCP或UDP的负载,作为套接字传输到目标端,当缓存大小不足时,线程会 ...
- BZOJ 1503 郁闷的出纳员(splay)
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1503 题意:给出一个数列(初始为空),给出一个最小值Min,当数列中的数字小于Min时自动 ...
- C# MVC框架初学者
推荐网站:http://blog.csdn.net/zhuyu19911016520/article/category/6318590
- 质量管理三个概念:QC、QA和QM,你能分得清吗?
今天这里谈的QC.QA和QM,不是岗位或职位,而是一种概念或质量管理的不同时期所关注的重点. 1.产品 早期的质量管理(工业化雏形期)侧重于对终产品的检测.测试,即QC(品质控制)时代. 这个时期的质 ...
- [SpringBoot] - 一份笔记
一. Spring Boot 入门 1. Spring Boot 简介 简化Spring应用开发的一个框架; 整个Spring技术栈的一个大整合; J2EE开发的一站式解决方案; 2. 微服务 201 ...
- C# String.Join 与 StringBuilder 对比,谁更快
String.Join 文档 StringBuilder 文档 这两天刷 Leedcode 做到一道 String 的题时突然想到这俩对比的问题,于是查了一下资料并简单对比了一下. 首先对于 ...
- LA 4080 战争和物流(最短路树)
https://vjudge.net/problem/UVALive-4080 题意:给出一个n个结点m条边的无向图,每条边上有一个正权.令c等于每对结点的最短路长度之和.不连通的两点的最短路长度视为 ...
- HDU 1711 Number Sequence(KMP模板)
http://acm.hdu.edu.cn/showproblem.php?pid=1711 这道题就是一个KMP模板. #include<iostream> #include<cs ...