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

来源:互联网 发布:数据库发展方向 编辑:程序博客网 时间:2024/05/29 09:01

我广搜学的不好,转一下题解以后看


转自

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
  1. #include <iostream>  
  2. #include <cstdio>  
  3. #include <cstring>  
  4. #include <queue>  
  5. using namespace std;  
  6.   
  7. #define NUM 505  
  8.   
  9. int arr[NUM][NUM];  
  10. int m,n;  
  11. char dir_c[5] = {'U','D','L','R'};  
  12. int dir[5][2] = {  
  13.     {-1,0},  ///UP  
  14.     {1,0}, ///DOWM  
  15.     {0,-1}, ///LEFT  
  16.     {0,1}   ///RIGHT  
  17. };  
  18.   
  19. struct Node  
  20. {  
  21.     int x;  
  22.     int y;  
  23.     int num_step;  
  24.     string step;  
  25.     Node(int i,int j){  
  26.         x = i;  
  27.         y = j;  
  28.         num_step = 0;  
  29.         step = "";  
  30.     }  
  31. };  
  32.   
  33. ///最优解步骤  
  34. string ans;  
  35. ///最优解步数  
  36. int num_ans;  
  37.   
  38. ///最优解标记变量  
  39. int is_finish = 0;  
  40.   
  41. /* 
  42. *检查当前节点是否合法 
  43. */  
  44. int check(Node tmp)  
  45. {  
  46.     int i = tmp.x;  
  47.     int j = tmp.y;  
  48.   
  49.     if(i < 1 || j < 1){  
  50.         return 0;  
  51.     }  
  52.   
  53.     if(i > m || j > n){  
  54.         return 0;  
  55.     }  
  56.   
  57.     if(1 == arr[i][j]){  
  58.         return 0;  
  59.     }  
  60.   
  61.     return 1;  
  62. }  
  63.   
  64. int bfs()  
  65. {  
  66.     queue<Node> Q_node;  
  67.     Node start(1,1);  
  68.     Node tmp(0,0);  
  69.   
  70.     Q_node.push(start);  
  71.   
  72.     while(false == Q_node.empty()){  
  73.         Node now = Q_node.front();  
  74.         Q_node.pop();  
  75.   
  76.         ///如果现在遇到了一个结束  
  77.         ///那么说明获得了最优的步数  
  78.         ///把获得结果的标记变量 IS_FINISH 设为1  
  79.         if(now.x == m && now.y == n){  
  80.             is_finish = 1;  
  81.             ///更新最优步数  
  82.             num_ans = now.num_step;  
  83.             ///将当前的解法和之前的最优解进行比较,取字典序小的那个  
  84.             if("" == ans){  
  85.                 ///如果是第一次记录,那么不用比较  
  86.                 ans = now.step;  
  87.             }else{  
  88.                 ///取字典序小的那个  
  89.                 if(ans > now.step){  
  90.                     ans = now.step;  
  91.                 }  
  92.             }  
  93.             continue;  
  94.         }  
  95.   
  96.         ///如果已经找到了最优解 而且当前求解的步数长于最优解  
  97.         ///那么直接跳出循环,不再进行判断  
  98.         if(is_finish){  
  99.             if(now.num_step > num_ans){  
  100.                 continue;  
  101.             }  
  102.         }  
  103.   
  104.         for(int i = 0; i < 4; i++){  
  105.             tmp.x = now.x+dir[i][0];  
  106.             tmp.y = now.y+dir[i][1];  
  107.             ///记录下一步方向  
  108.             tmp.step = now.step + dir_c[i];  
  109.             tmp.num_step = now.num_step + 1;  
  110.   
  111.             if(check(tmp)){  
  112.                 Q_node.push(tmp);  
  113.                 ///标记来时路线,不再进行判断  
  114.                 arr[tmp.x][tmp.y] = 1;  
  115.             }  
  116.         }  
  117.     }  
  118.     return 0;  
  119. }  
  120.   
  121. int main()  
  122. {  
  123.     scanf("%d%d",&m,&n);  
  124.     for(int i = 1; i <= m; i++){  
  125.         for(int j = 1; j <= n; j++){  
  126.             scanf("%1d",&arr[i][j]);  
  127.         }  
  128.     }  
  129.   
  130.     bfs();  
  131.   
  132.     printf("%d\n%s\n",num_ans,ans.c_str());  
  133.     return 0;  
  134. }  

原创粉丝点击