【题目大意】
在一个10*10的地图上,现在有一处谷仓发生火灾,奶牛们要在谷仓与湖之间建立一条救援通道,求救援通道的最小长度‘.’表示空地,‘R’表示石头,不能通过,‘B’表示火灾,‘L’表示湖。
【解题思路】
用BFS以谷仓为起点开始搜索,记录到达湖的最小长度。
【代码】
#include <cstdio>
#include <iostream>
#include <queue>
using namespace std;
int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0};
int sx,sy;
bool sym[20][20];
int d[20][20],a[20][20];
queue <int> X,Y;
void BFS()
{
X.push(sx);
Y.push(sy);
while (!X.empty())
{
int x=X.front();
int y=Y.front();
X.pop();Y.pop();
for (int i=0;i<4;i++)
{
int xx=x+dx[i];
int yy=y+dy[i];
if (x<1 || y<1 || x>10 || y>10 || sym[xx][yy] || a[xx][yy]==1) continue;
sym[xx][yy]=true;
X.push(xx);
Y.push(yy);
d[xx][yy]=d[x][y]+1;
}
}
}
int main()
{
//freopen("g.in","r",stdin);
//freopen("g.out","w",stdout);
for (int i=1;i<=10;i++)
for (int j=1;j<=10;j++)
{
char x;
cin>>x;
if (x=='B') sx=i,sy=j;
if (x=='L') a[i][j]=2;
if (x=='R') a[i][j]=1;
}
BFS();
int ans=999999999;
for (int i=1;i<=10;i++)
for (int j=1;j<=10;j++)
if (a[i][j]==2 && d[i][j]-1>=0 && d[i][j]-1<ans) ans=d[i][j]-1;
printf("%d",ans);
}