蓝桥T291(BFS + 输出路径)

http://lx.lanqiao.org/problem.page?gpid=T291

学霸的迷宫

时间限制:1.0s   内存限制:256.0MB

问题描述

  学霸抢走了大家的作业,班长为了帮同学们找回作业,决定去找学霸决斗。但学霸为了不要别人打扰,住在一个城堡里,城堡外面是一个二维的格子迷宫,要进城堡必须得先通过迷宫。因为班长还有妹子要陪,磨刀不误砍柴功,他为了节约时间,从线人那里搞到了迷宫的地图,准备提前计算最短的路线。可是他现在正向妹子解释这件事情,于是就委托你帮他找一条最短的路线。

输入格式

  第一行两个整数n, m,为迷宫的长宽。
  接下来n行,每行m个数,数之间没有间隔,为0或1中的一个。0表示这个格子可以通过,1表示不可以。假设你现在已经在迷宫坐标(1,1)的地方,即左上角,迷宫的出口在(n,m)。每次移动时只能向上下左右4个方向移动到另外一个可以通过的格子里,每次移动算一步。数据保证(1,1),(n,m)可以通过。

输出格式

  第一行一个数为需要的最少步数K。
  第二行K个字符,每个字符∈{U,D,L,R},分别表示上下左右。如果有多条长度相同的最短路径,选择在此表示方法下字典序最小的一个。

样例输入

Input Sample 1:
3 3
001
100
110

Input Sample 2:
3 3
000
000
000

样例输出

Output Sample 1:
4
RDRD

Output Sample 2:
4
DDRR

数据规模和约定

  有20%的数据满足:1<=n,m<=10
  有50%的数据满足:1<=n,m<=50
  有100%的数据满足:1<=n,m<=500。

分析:这题暴露了我的基础太差了,这题大约做了一个小时,提交了4遍,显示输入路径我傻逼的想着从终点往前深搜,然后还得考虑字典顺序结果不行,真是傻逼,字典顺序怎能可能从终点开始搜索呢,肯定得从起点开始才能保证字典顺序,傻逼的又从改成起点,提交有错误,傻逼的发现没回溯,思维啊思维,切水题的速度太慢,太慢,太慢!!!

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <queue>
 5 #include <algorithm>
 6 using namespace std;
 7 const int INF = 0x3f3f3f3f;
 8 char direct[4] = {‘D‘,‘L‘,‘R‘,‘U‘},path[10000];
 9 int n,m,dis[600][600],cnt;
10 int gx[4] = {1,0,0,-1};
11 int gy[4] = {0,-1,1,0};
12 char g[600][600];
13 struct point
14 {
15     int x,y;
16 };
17 void bfs(int x,int y)
18 {
19     queue<point> q;
20     point temp,temp2;
21     temp.x = x;
22     temp.y = y;
23     q.push(temp);
24     while(q.size())
25     {
26         temp2 = q.front();
27         q.pop();
28         if(temp2.x == n && temp2.y == m)
29         {
30             return;
31         }
32         for(int i = 0; i < 4; i++)
33         {
34             int fx = gx[i] + temp2.x;
35             int fy = gy[i] + temp2.y;
36             if(fx >= 1 && fx <= n && fy >= 1 && fy <= m && g[fx][fy] == ‘0‘ && dis[fx][fy] > dis[temp2.x][temp2.y] + 1)
37             {
38                 dis[fx][fy] = dis[temp2.x][temp2.y] + 1;
39                 temp.x = fx;
40                 temp.y = fy;
41                 q.push(temp);
42             }
43         }
44     }
45 }
46 int dfs(int x,int y,int ans)
47 {
48     if(x == n && y == m)
49     {
50         cnt = ans;
51         return true;
52     }
53     for(int i = 0; i < 4; i++)
54     {
55         int fx = x + gx[i];
56         int fy = y + gy[i];
57         if(fx >= 1 && fx <= n && fy <= m && fy >= 1 && dis[x][y] + 1 == dis[fx][fy])
58         {
59             path[ans] = direct[i];
60             if(dfs(fx,fy,ans + 1)) //不满足要回溯!
61                 return true;
62         }
63     }
64     return false;
65 }
66 int main()
67 {
68     scanf("%d%d", &n, &m);
69     getchar();
70     for(int i = 1; i <= n; i++)
71     {
72         for(int j = 1; j <= m; j++)
73         {
74             scanf("%c", &g[i][j]);
75         }
76         getchar();
77     }
78     memset(dis, INF, sizeof(dis));
79     dis[1][1] = 0;
80     cnt = 0;
81     bfs(1,1);
82     dfs(1,1,0);
83     printf("%d\n", dis[n][m]);
84     path[cnt] = ‘\0‘;
85     printf("%s\n", path);
86    return 0;
87 }

时间: 2025-01-02 04:43:22

蓝桥T291(BFS + 输出路径)的相关文章

Ignatius and the Princess I (hdu 1026 优先队列+bfs+输出路径)

Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 14624    Accepted Submission(s): 4634 Special Judge Problem Description The Princess has been abducted by the BEelzeb

(BFS 输出路径 pair)

迷宫问题 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 41913   Accepted: 23240 Description 定义一个二维数组: 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, 0, 1, 0, }; 它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走

迷宫问题 (BFS ?输出路径)

题目链接:http://poj.org/problem?id=3984 思路: 这道题的难点我觉得主要是在记录路径上面. 我们不能去记录当前的步数的走的坐标(x,y) ,因为这样会被后面的覆盖. 所以我们记录的应该是前一步所走的 具体代码: 1 #include <iostream> 2 #include <string> 3 #include <cstring> 4 #include <queue> 5 #include <string.h>

bfs输出路径 &amp;&amp; 最短路(迪杰斯特拉)输出路径

问题描述 解决方法 1.像第一个问题那就是最短路问题(我代码采用迪杰斯特拉算法)实现 2.换乘次数最少,那就用bfs广搜来寻找答案.但是我的代码不能保证这个最少换乘是最短路程 代码 1 #include<stdio.h> 2 #include<iostream> 3 #include<algorithm> 4 #include<string.h> 5 #include<queue> 6 #include<vector> 7 using

地下迷宫(bfs输出路径)

题解:开一个pre数组用编号代替当前位置,编号用结构题另存,其实也可以i*m+j来代替,我写的有点麻烦了; 代码: #include <iostream> #include <cstdio> #include <cmath> #include <algorithm> #include <cstring> #include <queue> using namespace std; #pragma(1) typedef struct No

hdu 1026 Ignatius and the Princess I(bfs搜索+输出路径)

题目来源:hdu-1026 Ignatius and the Princess I Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 14677 Accepted Submission(s): 4653 Special Judge Problem Description The Princ

Pots--poj(bfs,输出路径)

http://poj.org/problem?id=3414 题意: 给你两个容量为a,b的杯子:有3个操作: 1:FILL(i):把第i个杯子从水库中装满: 2:DROP(i):把第i个杯子清空: 3:POUR(i,j):把第i个杯子的水移入到j中,直到第i个杯子空了或者第j个杯子满了为止: 分析:本题和上篇的差不多,多的就是输出路径: 包含六个过程:水池—>a: 水池—>b:a->水池:b->水池:a->b:b->a: |  prea pre[x][y] |  pr

迷宫问题---poj3984(bfs,输出路径问题)

题目链接 主要就是输出路径问题: pre[x][y]表示到达(x,y)是由点(pre[x][y].x,  pre[x][y].y)而来: #include<stdio.h> #include<iostream> #include<string.h> #include<queue> #include<algorithm> using namespace std; #define N 220 #define INF 0xfffffff int dir

POJ 3414 Pot (输出路径)【BFS】

<题目链接> 题目大意: 有两个容量的空杯子,能够对这两个空杯子进行三种操作: 分别是fill(a),装满a杯子: drop(a),倒空a杯子: pour(a,b),将a杯子中的水倒入b杯子中: 现在问你,是否能够通过这三种操作,使得这两个杯子中至少有一个杯子中含有c体积的水,如果不行,输出"impossible",如果可以,输出操作的步数,以及每一步的具体操作. 解题分析: 此题与一道输出路径的很相像,只不过那道题是输出每一次操作对应的点的状态,而此题是要输出具体的操作.