bfs记录路径,蓝桥杯真题
题意:在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记录路径,蓝桥杯真题的更多相关文章
- 【蓝桥杯真题】地宫取宝(搜索->记忆化搜索详解)
链接 [蓝桥杯][2014年第五届真题]地宫取宝 题目描述 X 国王有一个地宫宝库.是 n x m 个格子的矩阵.每个格子放一件宝贝.每个宝贝贴着价值标签. 地宫的入口在左上角,出口在右下角. 小明被 ...
- Java实现 LeetCode 887 鸡蛋掉落(动态规划,谷歌面试题,蓝桥杯真题)
887. 鸡蛋掉落 你将获得 K 个鸡蛋,并可以使用一栋从 1 到 N 共有 N 层楼的建筑. 每个蛋的功能都是一样的,如果一个蛋碎了,你就不能再把它掉下去. 你知道存在楼层 F ,满足 0 < ...
- POJ.3894 迷宫问题 (BFS+记录路径)
POJ.3894 迷宫问题 (BFS+记录路径) 题意分析 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, ...
- 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 ...
- HDU1026--Ignatius and the Princess I(BFS记录路径)
Problem Description The Princess has been abducted by the BEelzebub feng5166, our hero Ignatius has ...
- 蓝桥杯vip题阶乘计算
蓝桥杯vip题阶乘计算 详细题目 输入一个正整数n,输出n!的值. 其中n!=123*-*n. 算法描述 n!可能很大,而计算机能表示的整数范围有限,需要使用高精度计算的方法.使用一个数组A来表示一个 ...
- Java实现UVA10131越大越聪明(蓝桥杯每周一题)
10131越大越聪明(蓝桥杯每周一题) [问题描述] 一些人认为,大象的体型越大,脑子越聪明.为了反驳这一错误观点,你想要分析一组大象的数据,找出尽量 多的大象组成一个体重严格递增但 IQ 严格递减的 ...
- hdu 1026 Ignatius and the Princess I(优先队列+bfs+记录路径)
以前写的题了,现在想整理一下,就挂出来了. 题意比较明确,给一张n*m的地图,从左上角(0, 0)走到右下角(n-1, m-1). 'X'为墙,'.'为路,数字为怪物.墙不能走,路花1s经过,怪物需要 ...
- 迷宫问题(bfs+记录路径)
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=105278#problem/K K - 迷宫问题 Time Limit:1000 ...
随机推荐
- maya cmds pymel undoInfo chunk 撤销束
maya cmds pymel undoInfo chunk 撤销束 cmds.undoInfo(openChunk = 1) # your code cmds.undoInfo(closeChunk ...
- 转载博文: Py西游攻关之IO model
Py西游攻关之IO model 转载:https://www.cnblogs.com/yuanchenqi/articles/5722574.html 事件驱动模型 上节的问题: 协程:遇到IO操作就 ...
- pip命令出现了问题,提示说找不到ssl模块
Could not find a version that satisfies the requirement pygame (from versions: ) No matching distrib ...
- 如何避免OOM
一.减小对象的内存占用 1)使用更加轻量的数据结构 例如,我们可以考虑使用ArrayMap/SparseArray而不是HashMap等传统数据结构. ArrayMap和HashMap主要不同之处在于 ...
- window编程_消息分类
Window应用程序利用Windows消息(Message)与应用程序及系统进行信息交换.消息分为:消息号:有事先定义好的消息名标识字节参数(wParam):用于提供消息的附加信息长字节参数(lPar ...
- 搭建自己的Webpack项目
五,搭建自己的Webpack项目 https://www.jianshu.com/p/42e11515c10f
- Android Notification 详解
转载自 http://www.cnblogs.com/travellife/  Notification 概述 Notification,是一种具有全局效果的通知,可以在系统的通知栏中显示.当 A ...
- MySQL错误码
MySQL运行异常时,查看服务日志可看到error number,该错误码为系统调用出错码,具体如下. errno.00 is: Success 成功 errno.01 is: Operatio ...
- 【并查集缩点+tarjan无向图求桥】Where are you @牛客练习赛32 D
目录 [并查集缩点+tarjan无向图求桥]Where are you @牛客练习赛32 D PROBLEM SOLUTION CODE [并查集缩点+tarjan无向图求桥]Where are yo ...
- 传统方式和插件方式 分别实现 分页 功能 pageHelper 插件
实现分页 这里提供两种方式 一种是传统的分页方式 一种是基于pageHelper插件 实现的分类 推荐使用后者 前者是一般开发的方式 思路 先手动创建一个 pageUtil 工具 ...