题意:在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. Prime Distance POJ - 2689 (数学 素数)

    The branch of mathematics called number theory is about properties of numbers. One of the areas that ...

  2. leetcode算法题整理

    一.线性表,如数组,单链表,双向链表 线性表.数组 U1.有序数组去重,返回新数组长度 A = [1,1,2] -> [1,2] 返回2   分析:其实一般数组的问题都可以用两个指针解决,一个指 ...

  3. spring-boot+mybatisPlus+shiro的集成demo 我用了5天

    spring-boot + mybatis-plus + shiro 的集成demo我用了五天 关于shiro框架,我还是从飞机哪里听来的,就连小贱都知道,可我母鸡啊.简单百度了下,结论很好上手,比s ...

  4. C语言一个程序的存储空间

    按区域划分: 堆区:自动分配内存区.//堆栈段 栈区:手动分配内存区.//堆栈段 全局(静态)区:静态变量和全局变量.//数据段(读写) 常量区:存放const全局变量和字符串常量.//数据段(只读) ...

  5. ApiShell

    using System; using System.Runtime.InteropServices; using HWND = System.IntPtr; using HANDLE = Syste ...

  6. js表单提交到后台对象接收

    $.extend({ StandardPost:function(url,args){ var form = $("<form method='post' target='_blank ...

  7. graph engine

    有个侥幸的机会,参与了微软的项目,侥幸的接触了,graph engine图形数据库,感觉很是新颖,做点记录,和大家分享,理解有限,发现不足之处,还请指点. 微软发分布式图处理引擎GraphEngine ...

  8. jQuery实现select级联

    使用Html5的数据属性(data-*)Map级联关系,代码如下: <!DOCTYPE html> <html> <head> <title>Selec ...

  9. laravel 控制器类DB类操作

    例子:TrGo表(trgo_chip): laravel框架建立:TrGoModel <?php namespace TrChaos\Model; class TrGoModel extends ...

  10. Java笔记--引用类型的使用

    使用引用类型的一般步骤: 1.导包:指定需要使用的目标在什么位置,在publicclass之前一行写代码 import 包名路径 2.创建:通常需要创建之才能使用,格式: 数据类型 变量名称 = ne ...