[蓝桥杯2019初赛]迷宫
思路下周补全
代码:
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
struct node
{
int x;
int y;
string p;
};
char a[31][51];
int st[31][51];
int dx[] = { 1,0,0,-1 }, dy[] = { 0,-1,1,0 };
char k[] = { 'D','L','R','U' };
void bfs()
{
node start;
start.x = 0, start.y = 0, start.p = "";
st[0][0] = 1;
queue<node> q;
q.push(start);
while (!q.empty())
{
node now = q.front();
q.pop();
if (now.x == 29 && now.y == 49)
{
cout << now.p << endl;
return;
}
for (int i = 0;i < 4;i++)
{
node next;
next.x = now.x + dx[i], next.y = now.y + dy[i];
if (next.x >= 0 && next.x < 30 && next.y >= 0 && next.y < 50 && !st[next.x][next.y] && a[next.x][next.y] != '1')
{
st[next.x][next.y] = 1;
next.p = now.p + k[i];
q.push(next);
}
}
}
}
int main()
{
for (int i = 0;i < 30;i++) cin >> a[i];
bfs();
return 0;
}