赞
踩
第十届蓝桥杯 ——迷宫_六级不考550+不改名-CSDN博客https://blog.csdn.net/weixin_46239370/article/details/105763944代码:
- #include <iostream>
- #include <cstring>
- #include <queue>
- using namespace std;
-
- typedef pair<int, int> PII;
-
- char g[30][50];
- int dist[30][50];
-
- char dir[] = {'D', 'L', 'R', 'U'};
- int dx[] = {1, 0, 0, -1}, dy[] = {0, -1, 1, 0};
-
- void bfs()
- {
- memset(dist, -1, sizeof dist);
- queue<PII> q;
- q.push({29, 49});
- dist[29][49] = 0;
-
- while(q.size())
- {
- PII t = q.front();
- q.pop();
-
- for (int i = 0; i < 4; i ++)
- {
- int a = t.first + dx[i], b = t.second + dy[i];
- if(a < 0 || a >= 30 || b < 0 || b >= 50) continue;
- if(g[a][b] == '1' || dist[a][b] != -1) continue;
- q.push({a, b});
- dist[a][b] = dist[t.first][t.second] + 1;
- }
- }
- }
-
- int main()
- {
- for (int i = 0; i < 30; i ++) cin >> g[i];
-
- bfs();
-
- string ans;
- int x = 0, y = 0;
- while(x != 29 || y != 49)
- {
- for (int i = 0; i < 4; i ++)
- {
- int a = x + dx[i], b = y + dy[i];
- if(a < 0 || a >= 30 || b < 0 || b >= 50) continue;
- if(g[a][b] == '1') continue;
- if(dist[x][y] == dist[a][b] + 1)
- {
- ans += dir[i];
- x = a, y = b;
- break;
- }
- }
- }
-
- cout << ans << endl;
- return 0;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。