Look for homework_BFS记录路径

1 篇文章 0 订阅

题目:

Description

 

 

 

Super scholar robs the all homework of others. The monitor decides to combat with the super scholar so as to help students to get back the homework. But super scholar lives in a castle because he doesn't want to be disturbed. The outside of the castle is a maze with two dimension grids. Entering the castle must pass the maze. The monitor needs to save time because of accompanying his girlfriend, so he wants to make a student named Huachao Wei help him who hasn't a girlfriend. Now he has the map of the maze and needs to calculate the shortest path.

Input

The input consists several cases.For each case starts with two integers n,m(​),symbolizing the length and width of the maze.The next n lines contain m numbers with no space and value for only 0 and 1.The essence is that 0 can pass ,1 can't pass. Now you are in the place of (1,1) refering to the top left corner.the export is in the place of (n,m).Each step can only walk with up,down,left and right.

Output

For each case,the first line prints the least number of steps to arrive the castle.The second line prints k characters for U,D,L,R,representing up,down,left,right.if there are many paths with the same length,please print the path with Minimum dictionary.It is guarantees that there must have a path for arriving the castle.

Sample Input

3 3
001
100
110
3 3
000
000
000

Sample Output

4
RDRD
4
DDRR

题意:

就是让你按照DLRU的优先顺序输出从(1,1)到(n,m)的路径

题解:

可以用结构体标记,每到一个点,这个点以后可以到达的路径就保存下来,直到到达终点,输出路径。

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<sstream>
#include<vector>
#include<map>
#include<queue>
#include<set>
#include<cmath>
#define up(i, x, y) for(int i = x; i <= y; i++)
#define down(i, x, y) for(int i = x; i >= y; i--)
#define MAXN ((int)1e1 + 10)
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
int n, m, k;
int a, b;
int x, y;
int T, t;
int len = 0;
char G[MAXN][MAXN];
struct node{int x, y, time;  //time辅助ans保存路径
 char ans[100 + 10];};  //保存路径
int vis[MAXN][MAXN];
int nxt[4][2] = {1, 0, 0, -1, 0, 1, -1, 0};
char step[4] = {'D', 'L', 'R', 'U'};
int main()
{
    while(~scanf("%d %d", &n, &m))
    {
        len = 0;
        up(i, 1, n)scanf("%s", G[i] + 1);
        queue<node> que;
        que.push(node{1, 1});
        G[1][1] = '1';
        while(!que.empty()){
            node t = que.front(); que.pop();
            node k = t;
            up(i, 0, 3)
            {
                k.x = t.x + nxt[i][0], k.y = t.y + nxt[i][1], k.time = t.time + 1;
                if(k.x >= 1 && k.x <= n && k.y >= 1 && k.y <= m && G[k.x][k.y] == '0'){
                    k.ans[k.time] = step[i];//保存路径
                    G[k.x][k.y] = '1';
                    que.push(k);
                }
                if(k.x == n && k.y == m){  //到达终点输出路径
                    cout<<k.time<<'\n'<<k.ans + 1<<'\n';
                    goto stop;
                }
            }
        }
        stop:{}
    }
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值