题意:在01矩阵中,找到一条从入口到终点的最短路径,并且打印这条路径。

题目链接:http://lx.lanqiao.cn/problem.page?gpid=T291

#include<iostream>
#include<queue>
#include<cstring>
using namespace std; int vis[][];
int map[][];
int n,m; struct node{
int x;
int y;
int dis; //到达这个点的距离
};
queue<node>q; struct father{
int x;
int y;
char dir; //到达这个点的方式
}path[][]; bool judge(node nn)
{
if(nn.x<||nn.x>n||nn.y<||nn.y>m||map[nn.x][nn.y]==||vis[nn.x][nn.y]==)
{
return false;
}
return true;
} char judge_dir(int k)
{
if(k==)
{
return 'D';
}
if(k==)
{
return 'L';
}
if(k==)
{
return 'R';
}
if(k==)
{
return 'U';
}
} int dx[]={,,,-};//下(D),左(L),右(R),上(U)
int dy[]={,-,,}; int bfs()
{
memset(vis,,sizeof(vis));
while(!q.empty())
{
q.pop();
}
node nn;
nn.x=;
nn.y=;
nn.dis=;
vis[][]=;
q.push(nn);
while(!q.empty())
{
node t=q.front();
q.pop();
for(int k=;k<;k++)
{
node next;
next.x=t.x+dx[k];
next.y=t.y+dy[k];
next.dis=t.dis+;
if(judge(next))
{
//cout<<"next "<<next.x<<' '<<next.y<<endl;
q.push(next);
vis[next.x][next.y]=;
path[next.x][next.y].x=t.x;
path[next.x][next.y].y=t.y;
path[next.x][next.y].dir=judge_dir(k);
if(next.x==n&&next.y==m)
{
return next.dis;
}
}
}
}
return -;
} void print_path(int xx,int yy)
{
if(xx==&&yy==)
{
return;
}
print_path(path[xx][yy].x,path[xx][yy].y);
//cout<<"xx"<<xx<<endl;
//cout<<"yy"<<yy<<endl;
cout<<path[xx][yy].dir; } int main()
{
cin>>n>>m;
getchar();
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
char ch=getchar();
map[i][j]=ch-'';
}
getchar();
}
int ans=bfs();
cout<<ans<<endl;
print_path(n,m);
return ;
}

bfs记录路径,蓝桥杯真题的更多相关文章

  1. 【蓝桥杯真题】地宫取宝(搜索->记忆化搜索详解)

    链接 [蓝桥杯][2014年第五届真题]地宫取宝 题目描述 X 国王有一个地宫宝库.是 n x m 个格子的矩阵.每个格子放一件宝贝.每个宝贝贴着价值标签. 地宫的入口在左上角,出口在右下角. 小明被 ...

  2. Java实现 LeetCode 887 鸡蛋掉落(动态规划,谷歌面试题,蓝桥杯真题)

    887. 鸡蛋掉落 你将获得 K 个鸡蛋,并可以使用一栋从 1 到 N 共有 N 层楼的建筑. 每个蛋的功能都是一样的,如果一个蛋碎了,你就不能再把它掉下去. 你知道存在楼层 F ,满足 0 < ...

  3. POJ.3894 迷宫问题 (BFS+记录路径)

    POJ.3894 迷宫问题 (BFS+记录路径) 题意分析 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, ...

  4. Codeforces-A. 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 ...

  5. HDU1026--Ignatius and the Princess I(BFS记录路径)

    Problem Description The Princess has been abducted by the BEelzebub feng5166, our hero Ignatius has ...

  6. 蓝桥杯vip题阶乘计算

    蓝桥杯vip题阶乘计算 详细题目 输入一个正整数n,输出n!的值. 其中n!=123*-*n. 算法描述 n!可能很大,而计算机能表示的整数范围有限,需要使用高精度计算的方法.使用一个数组A来表示一个 ...

  7. Java实现UVA10131越大越聪明(蓝桥杯每周一题)

    10131越大越聪明(蓝桥杯每周一题) [问题描述] 一些人认为,大象的体型越大,脑子越聪明.为了反驳这一错误观点,你想要分析一组大象的数据,找出尽量 多的大象组成一个体重严格递增但 IQ 严格递减的 ...

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

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

  9. 迷宫问题(bfs+记录路径)

    题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=105278#problem/K K - 迷宫问题 Time Limit:1000 ...

随机推荐

  1. jmeter之JDBC Request各种数据库配置

    URL和JDBC驱动: Datebase Driver class Database URL MySQL com.mysql.jdbc.Driver jdbc:mysql://host:port/{d ...

  2. HBuilder 的使用

    创建一个nui项目 打开index.html才能运行 查看夜神模拟器 创建App标题:mhe :在body中输入mhe让后回车,如果右侧不显示,Ctrl+s保存一下 创建标题:mhe 创建九宫格:mb ...

  3. ZAB协议与Paxos算法

    ZooKeeper并没有直接采用Paxos算法,而是采用一种被称为ZAB(ZooKeeper Atomic Broadcast)的一致性协议 ZooKeeper是一个典型的分布式数据一致性的解决方案, ...

  4. React(一)使用脚手架创建React项目

    1.安装脚手架 现在使用较多的就是这三种脚手架工具: react-boilerplate react-redux-starter-kit create-react-app 我使用的是第三种,faceb ...

  5. 19 ArcGIS Server 备份与恢复

    一.备份过程 1.找到已经安装的arcgis server安装目录,并找到备份工具: 2.快捷键win + R启动cmd,将备份工具文件拖入cmd窗口,enter 3. 通过backup.py脚本进行 ...

  6. Invalid bound statement (not found): com.xsw.dao.CategoryDao.getCategoryById] with root cause

    五月 30, 2018 11:11:03 上午 org.apache.catalina.core.StandardWrapperValve invoke严重: Servlet.service() fo ...

  7. 与postman的第一次亲密接触

    postman和JMeters是外部接口测试的两个工具,通过界面化的方法,来实现操作http报文携带的请求字段.VK值.cookie.header值及文件.本篇主要介绍postman.  首先,补充下 ...

  8. C++中的Public 、Private、Protected 区别

    第一: private,public,protected的访问范围: private: 只能由该类的成员函数.友元的成员函数访问,不能被其他类的成员函数访问,即使是该类的对象也不能直接访问 publi ...

  9. String工具类2

    1:比较字符串 public static void main(String[] args) { // String去创建对象有多种方式 // 方式1 直接字面值赋值 String s = " ...

  10. 【转载】Fiddler工具使用介绍(一)

    原文https://www.cnblogs.com/miantest/p/7289694.html(一) https://www.cnblogs.com/miantest/p/7290176.html ...