2019第十届蓝桥杯 E题 迷宫

  1 /*输入 
  2 30 50 
  3 01010101001011001001010110010110100100001000101010
  4 00001000100000101010010000100000001001100110100101
  5 01111011010010001000001101001011100011000000010000
  6 01000000001010100011010000101000001010101011001011
  7 00011111000000101000010010100010100000101100000000
  8 11001000110101000010101100011010011010101011110111
  9 00011011010101001001001010000001000101001110000000
 10 10100000101000100110101010111110011000010000111010
 11 00111000001010100001100010000001000101001100001001
 12 11000110100001110010001001010101010101010001101000
 13 00010000100100000101001010101110100010101010000101
 14 11100100101001001000010000010101010100100100010100
 15 00000010000000101011001111010001100000101010100011
 16 10101010011100001000011000010110011110110100001000
 17 10101010100001101010100101000010100000111011101001
 18 10000000101100010000101100101101001011100000000100
 19 10101001000000010100100001000100000100011110101001
 20 00101001010101101001010100011010101101110000110101
 21 11001010000100001100000010100101000001000111000010
 22 00001000110000110101101000000100101001001000011101
 23 10100101000101000000001110110010110101101010100001
 24 00101000010000110101010000100010001001000100010101
 25 10100001000110010001000010101001010101011111010010
 26 00000100101000000110010100101001000001000000000010
 27 11010000001001110111001001000011101001011011101000
 28 00000110100010001000100000001000011101000000110011
 29 10101000101000100010001111100010101001010000001000
 30 10000010100101001010110000000100101010001011101000
 31 00111100001000010000000110111000000001000000001011
 32 10000001100111010111010001000110111010101101111000
 33 */
 34 
 35 
 36 
 37 #include <cstdio>
 38 #include <iostream>
 39 #include <queue>
 40 #include <cstring> 
 41 using namespace std;
 42 
 43 int m, n;
 44 char map[60][60];
 45 bool vis[60][60];
 46 int dir[4][2] = {{1,0},{0,-1}, {0,1}, {-1,0}};//方向 下左右上 
 47 char dirc[4] = {'D', 'L', 'R', 'U'};
 48 
 49 struct node
 50 {
 51     int x;
 52     int y;
 53     int step;
 54     string str;
 55     node(int xx, int yy, int ss, string s)//构造函数 
 56     {
 57         x=xx; y=yy; step=ss; str=s;
 58     }
 59 };
 60 
 61 bool check(int x, int y)
 62 {
 63     if(x<0 || x>=m || y<0 || y>=n || vis[x][y] || map[x][y] == '1')
 64         return false;
 65     return true;
 66 }
 67 
 68 queue<node> q;
 69 
 70 void bfs(int x, int y)
 71 {
 72     q.push(node(0, 0, 0, ""));//起点入队
 73     vis[0][0]=true;
 74     while(!q.empty())
 75     {
 76         node now = q.front();
 77         if(now.x == m-1 && now.y == n-1)//到达终点 
 78         {
 79             cout<<now.str<<endl;
 80             cout<<now.step<<endl;
 81             break;
 82         }
 83         q.pop();
 84         for(int i=0; i<4; i++)
 85         {
 86             int xx=now.x+dir[i][0];
 87             int yy=now.y+dir[i][1];
 88             if(check(xx, yy))
 89             {
 90                 q.push(node(xx, yy, now.step+1, now.str+dirc[i]));
 91                 vis[xx][yy]=true;
 92             }
 93         } 
 94     }
 95     
 96 }
 97 
 98 int main()
 99 {
100     cin>>m>>n;
101     for(int i=0; i<m; i++)
102         scanf("%s", map[i]);
103     bfs(0, 0);
104     return 0;
105 }

题外话:

比赛时这题卡了一个多小时, 没做出来, 挺莫名其妙的, 前不久才做过类型题。

过了十多天才把这题给补了, 算是了却了一个心结(滑稽)。

猜你喜欢

转载自www.cnblogs.com/watsoncao/p/10673941.html