Pagini recente » Cod sursa (job #664366) | Cod sursa (job #2790988) | Cod sursa (job #995262) | Cod sursa (job #1516032) | Cod sursa (job #2570569)
#include <bits/stdc++.h>
#define pii pair <int,int>
#define x first
#define y second
#define mp make_pair
using namespace std;
ifstream f("barbar.in");
ofstream g("barbar.out");
int dl[]={-1,1,0,0};
int dc[]={0,0,-1,1};
int n,m,stX,stY,fnX,fnY,a[1<<10][1<<10],viz[1<<10][1<<10];
queue <pii> q;
bool Lee(int x)
{ if(a[stX][stY]<x)
return 0;
for(int i=1; i<=n; i++)
for(int j=1; j<=m; j++)
viz[i][j]=0;
viz[stX][stY]=1;
q.push(mp(stX,stY));
for(int i,j; !q.empty(); q.pop())
{ i=q.front().first,j=q.front().second;
for(int k=0; k<4; k++)
{ int ni=i+dl[k],nj=j+dc[k];
if(a[ni][nj]>=x && !viz[ni][nj] && a[ni][nj]!=1)
{ viz[ni][nj]=1;
q.push(mp(ni,nj));
}
}
}
return viz[fnX][fnY];
}
int main()
{ f>>n>>m;
for(int i=1; i<=n; i++)
{ string s;
f>>s;
for(int j=0; j<s.size(); j++)
if(s[j]=='I')
{ stX=i;
stY=j+1;
}
else
if(s[j]=='O')
{ fnX=i;
fnY=j+1;
}
else
if(s[j]=='D')
{ q.push(mp(i,j+1));
a[i][j+1]=1;
}
else
if(s[j]=='*')
a[i][j+1]=-1;
}
for(int i=1; i<=n; i++)
for(int j=1; j<=m; j++)
a[i][j]=(a[i][j] ? a[i][j] : 1<<30);
for(int i,j; !q.empty(); q.pop())
{ i=q.front().first,j=q.front().second;
for(int k=0; k<4; k++)
{ int ni=i+dl[k],nj=j+dc[k];
if(a[i][j]+1<a[ni][nj])
{ a[ni][nj]=a[i][j]+1;
q.push(mp(ni,nj));
}
}
}
int st=1,dr=n*n,sol=0;
while(st<=dr)
{ int mij=(st+dr)>>1;
if(Lee(mij))
{ st=mij+1;
sol=mij;
}
else
dr=mij-1;
}
g<<sol-1;
g.close(); return 0;
}