BFS模板
`#include<bits/stdc++.h>
using namespace std;
struct node
{
int x, y, step;
string s;
node(){}
node(int x, int y, int step, string s):x(x), y(y), step(step), s(s){}
};
queue
bool vis[100][100];
char Map[100][100];
int n = 50, m = 30;
int dir[][2] = {1,0, 0,-1, 0,1, -1,0};
char dir_s = "DLRU";
void bfs()
{
///1、把起点加入队列中(打标记)
q.push(node(1, 1, 0, ""));
vis[1][1] = 0;
///2、不断地取出队首元素,然后扩展即可
while(!q.empty())
{
node now = q.front();
q.pop();
if(now.x == n && now.y == m)
{
cout<<now.s<<endl;
break;
}
///3、四个方向扩展
for(int i = 0; i < 4; i++)
{
int next_x = now.x + dir[i][0];
int next_y = now.y + dir[i][1];
///4、3个约束 越界、已标记、障碍物
if(next_x < 1 || next_x > n || next_y < 1 || next_y > m)
continue;
if(vis[next_x][next_y])continue;
if(Map[next_x][next_y] == '1')continue;
q.push(node(next_x, next_y, now.step + 1, now.s + dir_s[i]));
vis[next_x][next_y] = 1;
}
}
}
`

浙公网安备 33010602011771号