Pagini recente » Cod sursa (job #3154697) | Cod sursa (job #854703) | Cod sursa (job #1588153) | Cod sursa (job #2162105) | Cod sursa (job #3149430)
#include <bits/stdc++.h>
using namespace std;
ifstream f("barbar.in");
ofstream g("barbar.out");
int n,m,i,j,l,max1,o1,o2,max2,I1,I2;
int w[1005][1005],le[1005][1005],v[1005][1005];
char t[1001];
int dx[]= {1,-1,0,0};
int dy[]= {0,0,1,-1};
struct pereche
{
int x,y;
};
pereche h1,h2,b;
queue <pereche> q;
stack <pereche> st;
bool interior(int x,int y)
{
return x>=1 && x<=n && y>=1 && y<=m;
}
void filln(int y)
{
v[I1][I2]=0;
st.push({I1,I2});
while(!st.empty())
{
b=st.top();
st.pop();
i=b.x;
j=b.y;
v[i][j] = 0;
if(v[i + 1][j]>= y)
st.push({i + 1, j});
if(v[i - 1][j] >= y)
st.push({i - 1, j});
if(v[i][j + 1] >= y)
st.push({i, j + 1});
if(v[i][j - 1] >= y)
st.push({i, j - 1});
}
}
int CautareBinara()
{
int mij,ok1,sol,st,dr;
st=1;
dr=max1;
sol=-1;
while(st<=dr)
{
mij=(st+dr)/2;
for(i=1; i<=n; i++)
for(j=1; j<=m; j++)
{
v[i][j]=le[i][j];
}
if(le[I1][I2]>=mij)
{
filln(mij);
}
if(v[o1][o2]==0)
{
sol=mij;
st=mij+1;
}
else
{
dr=mij-1;
}
}
return sol;
}
void leeD()
{
while(!q.empty())
{
h1=q.front();
q.pop();
for(l=0; l<=3; l++)
{
h2.x=h1.x+dx[l];
h2.y=h1.y+dy[l];
if(interior(h2.x,h2.y)&&(le[h1.x][h1.y]+1<le[h2.x][h2.y]||le[h2.x][h2.y]==0)&&w[h2.x][h2.y]==1)
{
le[h2.x][h2.y]=le[h1.x][h1.y]+1;
q.push(h2);
}
}
}
}
int main()
{
f>>n>>m;
for(i=1; i<=n; i++)
{
f>>t;
for(j=0; j<m; j++)
{
if(t[j]=='D')
{
w[i][j+1]=0;
h1.x=i;
h1.y=j+1;
q.push(h1);
}
if(t[j]=='*')
w[i][j+1]=-1;
if(t[j]=='.')
w[i][j+1]=1;
if(t[j]=='O')
{
w[i][j+1]=1;
o1=i;
o2=j+1;
}
if(t[j]=='I')
{
w[i][j+1]=1;
I1=i;
I2=j+1;
}
}
}
leeD();
for(i=1; i<=n; i++)
for(j=1; j<=m; j++)
{
v[i][j]=le[i][j];
}
for(i=1; i<=n; i++)
{
for(j=1; j<=n; j++)
{
max1=max(max1,le[i][j]);
}
}
le[o1][o2]=max1+1;
/* for(i=1; i<=n; i++)
{
for(j=1; j<=m; j++)
g<<le[i][j]<<" ";
g<<"\n";
}
g<<"\n";
*/
g<<CautareBinara();
return 0;
}