Pagini recente » Cod sursa (job #363975) | Cod sursa (job #1986080) | Cod sursa (job #3132186) | Cod sursa (job #3203082) | Cod sursa (job #1341627)
#include <fstream>
using namespace std;
const int N=1003;
const int dx[]={0,0,-1,1};
const int dy[]={1,-1,0,0};
struct poz
{
int x;
int y;
};
int cost[N][N];
bool umple[N][N];
poz q[4*N];
int n,m,xi,yi,xf,yf,p,u;
bool petabla (int x, int y)
{
if(x>=0 && x<n && y>=0 && y<m) return 1;
return 0;
}
void UMPLE (int x, int y, int min)
{
umple[x][y]=1;
int i,x1,y1;
for(i=0;i<4;i++)
{
x1=x+dx[i];
y1=y+dy[i];
if(petabla(x1,y1) && cost[x1][y1]!=-1 && cost[x1][y1]!=1 && cost[x1][y1]>=min && !umple[x1][y1])
UMPLE(x1,y1,min);
}
}
int Cautbin ()
{
int i,j,p=1<<27,sol=0;
while(p>0)
{
if(sol+p<=cost[xi][yi])
{
for(i=0;i<n;i++)
for(j=0;j<m;j++)
umple[i][j]=0;
UMPLE(xi,yi,sol+p);
if(umple[xf][yf])
sol+=p;
}
p>>=1;
}
return sol;
}
int main()
{
ifstream in("barbar.in");
ofstream out("barbar.out");
int i,j,x,y,rez;
char c;
in>>n>>m>>ws;
p=0; u=0;
for(i=0;i<n;i++)
for(j=0;j<m;j++)
{
in>>c>>ws;
if(c=='I')
{
xi=i;
yi=j;
}
if(c=='O')
{
xf=i;
yf=j;
}
if(c=='D')
{
q[u].x=i;
q[u].y=j;
cost[i][j]=1;
u++;
}
if(c=='*')
cost[i][j]=-1;
}
while(p!=u)
{
for(i=0;i<4;i++)
{
x=q[p].x+dx[i];
y=q[p].y+dy[i];
if(petabla(x,y) && cost[x][y]==0)
{
cost[x][y]=cost[q[p].x][q[p].y]+1;
q[u].x=x;
q[u].y=y;
u++;
}
}
p++;
}
rez=Cautbin()-1;
out<<rez;
return 0;
}