Pagini recente » Cod sursa (job #1189201) | Cod sursa (job #1704851)
#include <fstream>
#define N 1005
using namespace std;
ifstream q("barbar.in");
ofstream w("barbar.out");
int b[1005][1005],m,n,p,x2,y2,x1,y1;
char c[1005][1005];
struct ab{
int x,y;
};
ab v[1005*1005];
const int lin[]={0,0,1,-1},col[]={1,-1,0,0};
void lee(int x,int y){
for(int t=0;t<4;t++)
if(b[x+lin[t]][y+col[t]] == -2){
b[x+lin[t]][y+col[t]] = b[x][y]+1;
v[++v[0].x].x = x+lin[t];
v[v[0].x].y = y+col[t];
}
}
void lee2(int x,int y){
for(int t=0;t<4;t++)
if(c[x+lin[t]][y+col[t]] == -1 && b[x+lin[t]][y+col[t]] >= p){
v[++v[0].x].x = x+lin[t];
v[v[0].x].y = y+col[t];
c[x+lin[t]][y+col[t]] = 1;
}
}
void curat()
{
int i,j;
for(i=1;i<=m;i++)
for(j=1;j<=n;j++) c[i][j] = -1;
}
int solve(int lung){
int i;
v[0].x = 1;
p = lung;
v[1].x = x1;
v[1].y = y1;
curat();
if(b[x1][y1]<p)
return 0;
for(i=1;i<=v[0].x;i++){
if(c[x2][y2] == 1)
return 1;
lee2(v[i].x,v[i].y);
}
if(c[x2][y2] == 1)
return 1;
return 0;
}
int main(){
int i,j,st=0,dr=N*N*10,mij;
char x;
q>>m>>n;
for(i=1;i<=m;i++)
for(j=1;j<=n;j++){
q>>x;
if(x == 'D'){
v[++v[0].x].x = i;
v[v[0].x].y = j;
}
else
if(x == '*') b[i][j] = -1;
else b[i][j] = -2;
if(x == 'O'){
x2 = i;
y2 = j;
}
if(x == 'I'){
x1 = i;
y1 = j;
}
}
for(i=1;i<=v[0].x;i++)
lee(v[i].x,v[i].y);
while(st <= dr){
mij = (st+dr)/2;
if(solve(mij)) st = mij+1;
else
dr = mij-1;
}
w<<st-1;
return 0;
}