您的位置:首页 > 其它

蓝桥杯 算法提高 学霸的迷宫

2017-12-21 21:33 302 查看
我广搜学的不好,转一下题解以后看

转自
http://blog.csdn.net/liuxucoder/article/details/50114991
算法提高 学霸的迷宫  

时间限制: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。

代码如下:

[cpp] view
plain copy

#include <iostream>  

#include <cstdio>  

#include <cstring>  

#include <queue>  

using namespace std;  

  

#define NUM 505  

  

int arr[NUM][NUM];  

int m,n;  

char dir_c[5] = {'U','D','L','R'};  

int dir[5][2] = {  

    {-1,0},  ///UP  

    {1,0}, ///DOWM  

    {0,-1}, ///LEFT  

    {0,1}   ///RIGHT  

};  

  

struct Node  

{  

    int x;  

    int y;  

    int num_step;  

    string step;  

    Node(int i,int j){  

        x = i;  

        y = j;  

        num_step = 0;  

        step = "";  

    }  

};  

  

///最优解步骤  

string ans;  

///最优解步数  

int num_ans;  

  

///最优解标记变量  

int is_finish = 0;  

  

/* 

*检查当前节点是否合法 

*/  

int check(Node tmp)  

{  

    int i = tmp.x;  

    int j = tmp.y;  

  

    if(i < 1 || j < 1){  

        return 0;  

    }  

  

    if(i > m || j > n){  

        return 0;  

    }  

  

    if(1 == arr[i][j]){  

        return 0;  

    }  

  

    return 1;  

}  

  

int bfs()  

{  

    queue<Node> Q_node;  

    Node start(1,1);  

    Node tmp(0,0);  

  

    Q_node.push(start);  

  

    while(false == Q_node.empty()){  

        Node now = Q_node.front();  

        Q_node.pop();  

  

        ///如果现在遇到了一个结束  

        ///那么说明获得了最优的步数  

        ///把获得结果的标记变量 IS_FINISH 设为1  

        if(now.x == m && now.y == n){  

            is_finish = 1;  

            ///更新最优步数  

            num_ans = now.num_step;  

            ///将当前的解法和之前的最优解进行比较,取字典序小的那个  

            if("" == ans){  

                ///如果是第一次记录,那么不用比较  

                ans = now.step;  

            }else{  

                ///取字典序小的那个  

                if(ans > now.step){  

                    ans = now.step;  

                }  

            }  

            continue;  

        }  

  

        ///如果已经找到了最优解 而且当前求解的步数长于最优解  

        ///那么直接跳出循环,不再进行判断  

        if(is_finish){  

            if(now.num_step > num_ans){  

                continue;  

            }  

        }  

  

        for(int i = 0; i < 4; i++){  

            tmp.x = now.x+dir[i][0];  

            tmp.y = now.y+dir[i][1];  

            ///记录下一步方向  

            tmp.step = now.step + dir_c[i];  

            tmp.num_step = now.num_step + 1;  

  

            if(check(tmp)){  

                Q_node.push(tmp);  

                ///标记来时路线,不再进行判断  

                arr[tmp.x][tmp.y] = 1;  

            }  

        }  

    }  

    return 0;  

}  

  

int main()  

{  

    scanf("%d%d",&m,&n);  

    for(int i = 1; i <= m; i++){  

        for(int j = 1; j <= n; j++){  

            scanf("%1d",&arr[i][j]);  

        }  

    }  

  

    bfs();  

  

    printf("%d\n%s\n",num_ans,ans.c_str());  

    return 0;  

}  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: