Pagini recente » Cod sursa (job #2482911) | Cod sursa (job #2034927) | Cod sursa (job #1099768) | Cod sursa (job #1492619) | Cod sursa (job #2541568)
#include <bits/stdc++.h>
using namespace std;
ifstream f("barbar.in");
ofstream g("barbar.out");
int n,m,xs,ys,xf,yf,maxx,a[1005][1005],b[1005][1005];
int dx[]= {-1,0,1,0};
int dy[]= {0,1,0,-1};
char c;
queue<int>qx,qy;
inline void bordare()
{
for(int i=0; i<=n+1; i++)
a[i][0]=a[i][m+1]=-1;
for(int j=0; j<=m+1; j++)
a[0][j]=a[n+1][j]=-1;
}
inline void leeD()
{
while(!qx.empty())
{
int x=qx.front(),y=qy.front();
qx.pop();
qy.pop();
for(int i=0; i<4; i++)
{
int nx=x+dx[i],ny=y+dy[i];
if(a[nx][ny]==0)
{
a[nx][ny]=a[x][y]+1;
qx.push(nx);
qy.push(ny);
}
}
}
}
inline void lee(int val)
{
while(!qx.empty())
{
int x=qx.front(),y=qy.front();
qx.pop();
qy.pop();
for(int i=0; i<4; i++)
{
int nx=x+dx[i],ny=y+dy[i];
if(a[nx][ny]>=val && b[nx][ny]==0)
{
b[nx][ny]=b[x][y]+1;
qx.push(nx);
qy.push(ny);
}
}
}
}
inline void muta()
{
for(int i=1; i<=n; i++)
for(int j=1; j<=m; j++)
if(b[i][j]>0)
b[i][j]=0;
}
inline bool cond(int x)
{
qx.push(xs);
qy.push(ys);
lee(x);
if(b[xf][yf]>0)
return 1;
return 0;
}
inline void caut()
{
int st=1,dr=maxx,result=-1;
while(st<=dr)
{
int mij=(st+dr)/2;
if(cond(mij))
{
st=mij+1;
result=mij;
}
else
dr=mij-1;
muta();
}
g<<result;
}
int main()
{
f>>n>>m;
for(int i=1; i<=n; i++)
for(int j=1; j<=m; j++)
{
f>>c;
if(c=='D')
{
a[i][j]=1;
b[i][j]=-1;
qx.push(i);
qy.push(j);
}
else if(c=='*')
a[i][j]=b[i][j]=-1;
else if(c=='I')
{
xs=i;
ys=j;
}
else if(c=='O')
{
xf=i;
yf=j;
}
}
bordare();
leeD();
for(int i=1; i<=n; i++)
for(int j=1; j<=m; j++)
{
a[i][j]--;
if(a[i][j]>maxx)
maxx=a[i][j];
}
caut();
return 0;
}