版权声明:原创,未经作者允许禁止转载 https://blog.csdn.net/Mr_wuyongcong/article/details/83420562
正题
题目大意
一个迷宫,有许多出口,求一个点到最近的出口最远。
解题思路
直接bfs暴力搜索,然后保存上次的答案
code
// luogu-judger-enable-o2
#include<cstdio>
#include<queue>
#include<cstring>
#define N 210
using namespace std;
struct node{
int x,y;
};
const int dx[4]={0,0,1,-1},dy[4]={1,-1,0,0};
int a[N][N],w,h,maxs;
char map[N][N];
queue<node> q;
void bfs(int xs,int ys)//搜索
{
q.push((node){xs,ys});
a[xs][ys]=0;
while(!q.empty())
{
node x=q.front();q.pop();
int ws=a[x.x][x.y];
for(int i=0;i<4;i++) {
int xx=dx[i]+x.x,yy=dy[i]+x.y;
int zx=dx[i]*2+x.x,zy=dy[i]*2+x.y;
if(zx<=h&&zx>0&&zy<=w&&zy>0&&
map[xx][yy]==' '&&a[zx][zy]>ws+1)
{
q.push((node){zx,zy});
a[zx][zy]=ws+1;
}
}
}
}
int main()
{
memset(a,127/3,sizeof(a));
scanf("%d%d",&w,&h);
w=w*2+1;h=h*2+1;
for(int i=0;i<=h;i++)
{
gets(map[i]+1);
for(int j=1;j<=w;j++)
if(map[i][j]=='-'||map[i][j]=='+'||map[i][j]=='|')
map[i][j]='+';
else map[i][j]==' ';
}
for(int i=1;i<=h;i++)
{
if(map[i][1]==' ')
bfs(i,2);
if(map[i][w]==' ')
bfs(i,w-1);
}
for(int i=1;i<=w;i++)
{
if(map[1][i]==' ')
bfs(2,i);
if(map[h][i]==' ')
bfs(h-1,i);
}
for(int i=1;i<=h;i++)
//{
for(int j=1;j<=w;j++)
//{
if(a[i][j]<707406378)
maxs=max(maxs,a[i][j]);
//printf("%5d",a[i][j]);
//else printf("%5d",100);
//}printf("\n");}
printf("%d",maxs+1);
}