Pagini recente » Cod sursa (job #2964583) | Cod sursa (job #1987790) | Cod sursa (job #1076386) | Cod sursa (job #852439) | Cod sursa (job #2328052)
#include <fstream>
using namespace std;
ifstream in("barbar.in");
ofstream out("barbar.out");
const int N=1000;
const int dl[]= {-1,0,1,0};
const int dc[]= {0,-1,0,1};
struct punct
{
int l,c;
};
punct x,y,xi,xf,q[N*N];
bool b[N][N];
int a[N][N];
int n,m;
bool exista_drum(int d)
{
int st=0,dr=-1;
if (a[xi.l][xi.c] < d)
{
return false;
}
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
b[i][j] = false;
}
}
q[++dr] = xi;
while(st<=dr)
{
x=q[st++];
for(int i=0; i<4; i++)
{
y.l=x.l+dl[i];
y.c=x.c+dc[i];
if(a[y.l][y.c] >= d && !b[y.l][y.c])
{
q[++dr]=y;
b[y.l][y.c] = true;
}
}
}
return b[xf.l][xf.c];
}
int main()
{
char lin[1000];
in>>n>>m;
int st=0,dr=-1;
for(int i=0; i<=n+1; i++)
a[i][0]=-2,a[i][m+1]=-2;
for(int j=0;j<=m+1;j++)
a[0][j]=-2,a[n+1][j]=-2;
for(int i=1; i<=n; i++)
{
in>>(lin+1);
for(int j=1; j<=m; j++)
{
if(lin[j]=='.')
a[i][j]=-1;
else if(lin[j]=='*')
a[i][j]=-2;
else if(lin[j]=='I')
a[i][j]=-1,xi=(punct)
{
i,j
};
else if(lin[j]=='O')
a[i][j]=-1,xf=(punct)
{
i,j
};
else
{
q[++dr]=(punct)
{
i,j
};
a[i][j]=0;
}
}
}
while(st<=dr)
{
x=q[st++];
for(int i=0; i<4; i++)
{
y.l=x.l+dl[i];
y.c=x.c+dc[i];
if(a[y.l][y.c]==-1)
{
q[++dr]=y;
a[y.l][y.c]=1+a[x.l][x.c];
}
}
}
st=0,dr=2000;
while(st<dr)
{
int mij=(st+dr)/2;
if(exista_drum(mij))
st=mij+1;
else dr=mij;
}
/*
for(int i=1; i<=n; i++)
{
for(int j=1; j<=n; j++)
out<<a[i][j]<<" ";
out<<"\n";
}
*/
out << st - 1;
return 0;
}