蓝桥T291(BFS + 输出路径)
接下来n行,每行m个数,数之间没有间隔,为0或1中的一个。0表示这个格子可以通过,1表示不可以。假设你现在已经在迷宫坐标(1,1)的地方,即左上角,迷宫的出口在(n,m)。每次移动时只能向上下左右4个方向移动到另外一个可以通过的格子里,每次移动算一步。数据保证(1,1),(n,m)可以通过。
第二行K个字符,每个字符∈{U,D,L,R},分别表示上下左右。如果有多条长度相同的最短路径,选择在此表示方法下字典序最小的一个。
3 3
001
100
110
Input Sample 2:
3 3
000
000
000
4
RDRD
Output Sample 2:
4
DDRR
有50%的数据满足:1<=n,m<=50
有100%的数据满足:1<=n,m<=500。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
const int INF = 0x3f3f3f3f;
char direct[] = {'D','L','R','U'},path[];
int n,m,dis[][],cnt;
int gx[] = {,,,-};
int gy[] = {,-,,};
char g[][];
struct point
{
int x,y;
};
void bfs(int x,int y)
{
queue<point> q;
point temp,temp2;
temp.x = x;
temp.y = y;
q.push(temp);
while(q.size())
{
temp2 = q.front();
q.pop();
if(temp2.x == n && temp2.y == m)
{
return;
}
for(int i = ; i < ; i++)
{
int fx = gx[i] + temp2.x;
int fy = gy[i] + temp2.y;
if(fx >= && fx <= n && fy >= && fy <= m && g[fx][fy] == '' && dis[fx][fy] > dis[temp2.x][temp2.y] + )
{
dis[fx][fy] = dis[temp2.x][temp2.y] + ;
temp.x = fx;
temp.y = fy;
q.push(temp);
}
}
}
}
int dfs(int x,int y,int ans)
{
if(x == n && y == m)
{
cnt = ans;
return true;
}
for(int i = ; i < ; i++)
{
int fx = x + gx[i];
int fy = y + gy[i];
if(fx >= && fx <= n && fy <= m && fy >= && dis[x][y] + == dis[fx][fy])
{
path[ans] = direct[i];
if(dfs(fx,fy,ans + )) //不满足要回溯!
return true;
}
}
return false;
}
int main()
{
scanf("%d%d", &n, &m);
getchar();
for(int i = ; i <= n; i++)
{
for(int j = ; j <= m; j++)
{
scanf("%c", &g[i][j]);
}
getchar();
}
memset(dis, INF, sizeof(dis));
dis[][] = ;
cnt = ;
bfs(,);
dfs(,,);
printf("%d\n", dis[n][m]);
path[cnt] = '\0';
printf("%s\n", path);
return ;
}
蓝桥T291(BFS + 输出路径)的更多相关文章
- hdu 1026(BFS+输出路径) 我要和怪兽决斗
http://acm.hdu.edu.cn/showproblem.php?pid=1026 模拟一个人走迷宫,起点在(0,0)位置,遇到怪兽要和他决斗,决斗时间为那个格子的数字,就是走一个格子花费时 ...
- poj 3414 Pots(bfs+输出路径)
Description You are given two pots, having the volume of A and B liters respectively. The following ...
- bfs输出路径 && 最短路(迪杰斯特拉)输出路径
问题描述 解决方法 1.像第一个问题那就是最短路问题(我代码采用迪杰斯特拉算法)实现 2.换乘次数最少,那就用bfs广搜来寻找答案.但是我的代码不能保证这个最少换乘是最短路程 代码 1 #includ ...
- 地下迷宫(bfs输出路径)
题解:开一个pre数组用编号代替当前位置,编号用结构题另存,其实也可以i*m+j来代替,我写的有点麻烦了; 代码: #include <iostream> #include <cst ...
- POJ 3414 Pot (输出路径)【BFS】
<题目链接> 题目大意: 有两个容量的空杯子,能够对这两个空杯子进行三种操作: 分别是fill(a),装满a杯子: drop(a),倒空a杯子: pour(a,b),将a杯子中的水倒入b杯 ...
- 迷宫问题---poj3984(bfs,输出路径问题)
题目链接 主要就是输出路径问题: pre[x][y]表示到达(x,y)是由点(pre[x][y].x, pre[x][y].y)而来: #include<stdio.h> #includ ...
- Pots--poj(bfs,输出路径)
http://poj.org/problem?id=3414 题意: 给你两个容量为a,b的杯子:有3个操作: 1:FILL(i):把第i个杯子从水库中装满: 2:DROP(i):把第i个杯子清空: ...
- UVA-816.Abbott's Tevenge (BFS + 打印路径)
本题大意:给定一个迷宫,让你判断是否能从给定的起点到达给定的终点,这里起点需要输入起始方向,迷宫的每个顶点也都有行走限制,每个顶点都有特殊的转向约束...具体看题目便知... 本题思路:保存起点和终点 ...
- hdu 1026 bfs+记录路径
题意:从0,0点出发到n-1,m-1点,路上的数字代表要在这个点额外待多少秒,求最短的路 递归输出路径即可 #include<cstdio> #include<iostream> ...
随机推荐
- Redis缓存数据库详解
Redis最为常用的数据类型主要有以下五种: 1)String 2)Hash 3)List 4)Set 5)Sorted set 在具体描述这几种数据类型之前,我们先通过一张图了解下Redis内部内存 ...
- poj3371
Flesch Reading Ease Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2269 Accepted: 710 De ...
- POJ 1088滑雪
滑雪 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 89168 Accepted: 33474 Description ...
- C#文件和文件文件夹按时间、名称排序-顺序与倒序
对于文件和文件夹有多种排序方式,常用的就是按创建或修改时间.按文件名排序.在 C# 中,按时间和文件名排序都十分简单,用数组提供的排序方法 Array.Sort() 一行代码就可以搞定,当然也可以用常 ...
- MSMQ消息队列安装
一.Windows 7安装.管理消息队列1.安装消息队列 执行用户必须要有本地 Administrators 组中的成员身份,或等效身份. 具体步骤: 开始—>控制面板—>程 ...
- CSS3 chart
利用CSS3技术生成统计图. 原理:利用元素的百分比算出旋转度数.类似于斗地主时,手拿扑克牌的形状. 程序源码: <!DOCTYPE html> <html> <head ...
- [CareerCup] 1.5 Compress String 压缩字符串
1.5 Implement a method to perform basic string compression using the counts of repeated characters. ...
- Linux第二次学习笔记
#Linux第二次实验(第三周) 学习目标 熟悉Linux系统下的开发环境 熟悉vi的基本操作 熟悉gcc编译器的基本原理 熟练使用gcc编译器的常用选项 熟练使用gdb调试技术 熟悉makefile ...
- iOS开发UI篇—popoverController简单介绍(ipad)
一.简单介绍 1.什么是UIPopoverController 是iPad开发中常见的一种控制器(在iPhone上不允许使用) 跟其他控制器不一样的是,它直接继承自NSObject,并非继承自UIVi ...
- Jenkins进阶系列之——01使用email-ext替换Jenkins的默认邮件通知
1 简述 众所周知,Jenkins默认提供了一个邮件通知,能在构建失败.构建不稳定等状态后发送邮件.但是它本身有很多局限性,比如它的邮件通知无法提供详细的邮件内容.无法定义发送邮件的格式.无法定义灵活 ...