专题链接:https://codeforces.com/group/5yyKg9gx7m/contest/269908/problem/G
思路
简单的bfs,没啥好说的,套板子就完事了
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<string>
#include<map>
#include<cstring>
#include<climits>
#include<vector>
#include<queue>
#include<stack>
#define lowbit(i) ((i)&(-(i)))
#define ll long long
using namespace std;
int f[5][2] = { {0,0}, {0,1}, {0,-1}, {-1,0},{1,0} };//代表四个方向
char m[11][11];
int nx, ny;
struct node
{
int x;
int y;
int co;
}t,u;
queue <node>que;
int ans;
void bfs()
{
while (que.size())
{
u = que.front();
que.pop();
for (int k = 1; k <= 4; k++)
{
nx = u.x + f[k][0];
ny = u.y + f[k][1];
if ((nx <=10) && (nx >=1) && (ny <=10) && (ny >=1 ))
{
if (m[nx][ny] == '.')
{
m[nx][ny] = '*';
t.x = nx;
t.y = ny;
t.co = u.co + 1;
que.push(t);
}
else if (m[nx][ny] == 'B')
{
ans = u.co;
return;
}
}
}
}
};
int main()
{
for (int i = 1; i <= 10; i++)
scanf("%s", m[i] + 1);
for (int i = 1; i <=10; i++)
{
for (int j = 1; j <= 10; j++)
{
if (m[i][j] == 'L')
{
t.x = i;
t.y = j;
t.co = 0;
que.push(t);
bfs();
printf("%d\n",ans);
return 0;
}
}
}
return 0;
}