Pagini recente » Cod sursa (job #133587) | Cod sursa (job #2021437) | Cod sursa (job #777664) | Cod sursa (job #2283019) | Cod sursa (job #2947546)
#include <fstream>
#include <queue>
using namespace std;
ifstream fin("barbar.in");
ofstream fout("barbar.out");
int n,i,j,m,a[1005][1005],d[1005][1005],k,xx,yy,x,y,st,dr,mij,v[1005][1005],sol;
char ch;
const int dx[]={-1,0,1,0};
const int dy[]={0,1,0,-1};
queue <pair <int,int>> q;
pair <int,int> p,u;
int verif(int mij)
{
int i,j,xx,yy,x,y;
for (i=0;i<n;i++)
{
for (j=0;j<m;j++)
{
v[i][j]=-1;
}
}
v[p.first][p.second]=0;
q.push(p);
while (!q.empty())
{
x=q.front().first;
y=q.front().second;
q.pop();
for (k=0;k<4;k++)
{
xx=dx[k]+x;
yy=dy[k]+y;
if (xx>=1 && xx<=n && yy>=1 && yy<=m && a[xx][yy]==0 && d[xx][yy]>=mij && v[xx][yy]==-1)
{
v[xx][yy]=v[x][y]+1;
q.push({xx,yy});
}
}
}
if (v[u.first][u.second]>-1) return 1;
else return 0;
}
int main()
{
fin >>n>>m;
for (i=1;i<=n;i++)
{
for (j=1;j<=m;j++)
{
fin >>ch;
d[i][j]=-1;
if (ch=='*') a[i][j]=1;
else a[i][j]=0;
if (ch=='D')
{
q.push({i,j});
d[i][j]=0;
}
if (ch=='I')
{
p={i,j};
}
if (ch=='O')
{
u={i,j};
}
}
}
while (!q.empty())
{
x=q.front().first;
y=q.front().second;
q.pop();
for (k=0;k<4;k++)
{
xx=x+dx[k];
yy=y+dy[k];
if (xx>=1 && xx<=n && yy>=1 && yy<=m && a[xx][yy]==0 && d[xx][yy]==-1)
{
d[xx][yy]=d[x][y]+1;
q.push({xx,yy});
}
}
}
sol=-1;
st=1;
dr=1000000;
while (st<=dr)
{
mij=(st+dr)/2;
if (verif(mij)==1) {sol=mij;st=mij+1;}
else dr=mij-1;
}
fout <<sol;
return 0;
}