Pagini recente » Cod sursa (job #877705) | Cod sursa (job #879352) | Cod sursa (job #1272981) | Cod sursa (job #105565) | Cod sursa (job #2300085)
#include <bits/stdc++.h>
using namespace std;
ifstream in("barbar.in");
ofstream out("barbar.out");
const int N=1005,M=16;
int dx[]= {-1,0,1,0};
int dy[]= {0,-1,0,1};
struct poz
{
int x;
int y;
};
char a[N][N];
bool f[N][N];
int b[N][N],n,m;
poz start,stop,aux;
queue <poz> q;
void read()
{
in>>n>>m;
for(int i=1; i<=n; ++i)
{
for(int j=1; j<=m; ++j)
{
in>>a[i][j];
if(a[i][j]=='I')
{
start.x=i;
start.y=j;
}
else if(a[i][j]=='O')
{
stop.x=i;
stop.y=j;
}
else if(a[i][j]=='D')
{
aux.x=i;
aux.y=j;
q.push(aux);
b[i][j]=1;
}
}
}
}
void border()
{
for(int i=0; i<=n+1; ++i)
{
a[i][0]=a[i][n+1]='*';
}
for(int j=0; j<=m+1; ++j)
{
a[0][j]=a[n+1][j]='*';
}
}
void lee()
{
border();
poz k1,k2;
while(!q.empty())
{
k1=q.front();
q.pop();
for(int i=0; i<4; ++i)
{
k2.x=k1.x+dx[i];
k2.y=k1.y+dy[i];
if(a[k2.x][k2.y]!='*')
{
if(b[k2.x][k2.y]==0)
{
q.push(k2);
b[k2.x][k2.y]=b[k1.x][k1.y]+1;
}
}
}
}
}
bool check(int t)
{
if(b[start.x][start.y]<t)
{
return false;
}
for(int i=0;i<=n+1;++i)
for(int j=0;j<=m+1;++j)
f[i][j]=0;
while(!q.empty())
{
q.pop();
}
poz k1,k2;
q.push(start);
f[start.x][start.y]=1;
while(!q.empty())
{
k1=q.front();
q.pop();
for(int i=0; i<4; ++i)
{
k2.x=k1.x+dx[i];
k2.y=k1.y+dy[i];
if(b[k1.x][k1.y]>t&&a[k2.x][k2.y]!='*'&&f[k2.x][k2.y]!=1)
{
if(k2.x==stop.x&&k2.y==stop.y)
{
return true;
}
f[k2.x][k2.y]=1;
q.push(k2);
}
}
}
return false;
}
int binarySearch()
{
int ans=0;
int pas=1<<M;
while(pas>0)
{
if(check(ans+pas))
ans+=pas;
pas/=2;
}
return ans;
}
int main()
{
read();
/*while(!q.empty())
{
out<<q.front().x<<" "<<q.front().y<<'\n';
q.pop();
}*/
lee();
out<<binarySearch();
return 0;
}