```cpp
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<iomanip>
#include<vector>
#include<queue>
using namespace std;
#define RG register
#define IL inline
#define File(a) freopen(a".in","r",stdin);freopen(a".out","w",stdout)
template<typename T>
T qget(void)
{
T xi=0;
char ch=getchar();
bool f=false;
while(ch<'0'||ch>'9')
{
if(ch=='-')f=1;
ch=getchar( );
}
while( ch>='0'&&ch<='9')xi=xi*10+ch-48,ch=getchar();
if(f)xi=-xi;
return xi;
}
struct point
{
int x,y,B;
point()
{
B=0;
}
void set(int x,int y)
{
x=x,y=y;
}
point(int x,int y,int B)
{
x=x,y=y,B=B;
}
};
char map[501][501];
int se[501][501];
bool visit[501][501];
int fx[4]= {0,0,1,-1}
,fy[4]= {1,-1,0,0},w,h;
void update(point io)
{
se[io.x][io.y]=min(se[io.x][io.y],io.B+1);
}
bool cango(point io)
{
if(io.x>0&&io.x<=w&&io.y>0&&io.y<=h&&!visit[io.x][io.y]&&map[io.x][io.y]==' ')return true;
return false;
}
void bfs(queue<point>&k)
{
while(!k.empty())
{
point _new=k.front();
k.pop();
for(int i=0; i<4; i++)
{
point k1(_new.x+fx[i],_new.y+fy[i],_new.B+1);
if(cango(k1))
{
se[k1.x][k1.y]=min(se[k1.x][k1.y],k1.B);
visit[k1.x][k1.y]=1;
k.push(k1);
}
}
}
}
int main()
{
w=qget<int>(),h=qget<int>();
w=w*2+1,h=h*2+1;
vector<point>start;
for(int i=1; i<=w; i++)
for(int j=1; j<=h; j++)
{
scanf("%c",&map[i][j]);
if((i==1||j==1||i==w||j==h)&&map[i][j]==' ')start.push_back((point){i,j,0});
}
queue<point>k;
for(int i=1; i<=500; i++)for(int j=1; j<=500; j++)se[i][j]=1000000;
for(int i=0; i<(signed)start.size(); i++)
{
memset(visit,0,sizeof(visit));
k.push(start[i]);
bfs(k);
}
int maxn=-1;
for(int i=1; i<=w; i++)for(int j=1; j<=h; j++)
if(se[i][j]!=1000000)maxn=max(maxn,se[i][j]);
cout<<(maxn+1)/2;
return 0;
}
```
by ACAね @ 2017-06-09 18:41:36
正解:
```cpp
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<cstring>
using namespace std;
int h,w,js=1,ans=0;
char a[500][500];
int bj[3],b[3],dj[10000],dj1[10000],dj2[10000];
int f[500][500],n,m;
bool l[500][500];
int fx[10]= {0,0,0,1,-1};
int fy[10]= {0,1,-1,0,0};
int bfs(int x,int y) {
int head=0,tail=1;
dj[1]=x;
dj1[1]=y;
dj2[1]=0;
do {
head++;
for(int i=1; i<=4; i++) {
x=dj[head]+fx[i];
y=dj1[head]+fy[i];
if(a[x][y]==' '&&x>0&&x<=n&&y>0&&y<=m&&l[x][y]==0) {
tail++;
f[x][y]=min(f[x][y],dj2[head]+1);
dj[tail]=x;
dj1[tail]=y;
dj2[tail]=dj2[head]+1;
l[x][y]=1;
}
}
} while(head<tail);
}
int main() {
freopen("overfencing.in","r",stdin);
freopen("overfencing.out","w",stdout);
for(int i=1; i<=500; i++)
for(int j=1; j<=500; j++)
f[i][j]=10000;
cin>>w>>h;
char c;
n=2*h+1;
m=2*w+2;
for(int i=1; i<=n; i++) {
for(int j=1; j<=m; j++) {
scanf("%c",&a[i][j]);
if((i==1||j==2||i==n||j==m)&&a[i][j]==' ') {
b[js]=i;
bj[js]=j;
js++;
}
}
}
int ja=0;
for(int i=1; i<js; i++) {
memset(l,0,sizeof(l));
bfs(b[i],bj[i]);
}
for(int i=1; i<=n; i++)
for(int j=1; j<=m; j++)
if(f[i][j]!=10000)
ja=max(f[i][j],ja);
cout<<(ja+1)/2;
return 0;
}
```
by 天·三玖 @ 2017-06-09 20:52:26
@[LLCS](/space/show?uid=19730) @[Adscn](/space/show?uid=19607) 自己回自己的贴好玩吗= =
by 中2少年雷耶斯 @ 2017-06-10 12:07:58
@[LLCS](/space/show?uid=19730) 我和你写法差不多。一直wa
by MrWilliam @ 2017-08-27 16:24:26