Pagini recente » Cod sursa (job #2252109) | Cod sursa (job #2796102) | Cod sursa (job #1046756) | Cod sursa (job #2701993) | Cod sursa (job #386341)
Cod sursa(job #386341)
# include <fstream>
# include <iostream>
using namespace std;
struct nod {
int i, j;
nod *next;};
int a[1003][1003], n, m, d[1003][1003], ist, jst, ifin, jfin, c[1003][1003];
int di[4]={0, 0, 1, -1}, dj[4]={-1, 1, 0, 0};
nod *st, *dr;
void read ()
{
nod *t;
st=dr=new nod;
st->next=NULL;
ifstream fin ("barbar.in");
fin>>n>>m;
char c;
for (int i=1;i<=n;i++)
for (int j=1;j<=m;j++)
{
fin>>c;
if (c=='.') a[i][j]=1;
else if (c=='*') a[i][j]=-1;
else if (c=='D')
{a[i][j]=0;t=new nod;t->i=i;t->j=j;t->next=NULL;dr->next=t;dr=t;}
else if (c=='I'){a[i][j]=1;ist=i;jst=j;}
else if (c=='O'){a[i][j]=1;ifin=i;jfin=j;}
}
t=st;
st=st->next;
delete t;
}
int IN_M(int i, int j)
{
if (i<=n && i>0 && j<=m && j>0)
return 1;
return 0;
}
void distante ()
{
int i, j, ii, jj, k;
while (st)
{
i=st->i;
j=st->j;
for (k=0;k<4;k++)
{
ii=i+di[k];
jj=j+dj[k];
if (d[ii][jj]==0 && a[ii][jj]==1 && IN_M(ii, jj))
{
d[ii][jj]=d[i][j]+1;
nod *t=new nod;
t->i=i;
t->j=j;
t->next=NULL;
dr->next=t;
dr=t;
}
}
nod *q=st;
st=st->next;
delete q;
}
}
void drum ()
{
int k, i, j, ii, jj;
nod *t, *st, *dr;
st=dr=new nod;
st->i=ist;
st->j=jst;
st->next=NULL;
c[ist][jst]=d[ist][jst];
while (st)
{
i=st->i;
j=st->j;
for (k=0;k<4;k++)
{
ii=i+di[k];
jj=j+dj[k];
if (IN_M(ii, jj) && a[ii][jj]!=-1)
{
if (c[ii][jj]==0)
{
if (ii!=ifin && jj!=jfin)
t=new nod;t->i=ii;t->j=jj;t->next=NULL;dr->next=t;dr=t;
if (d[ii][jj]<c[i][j])c[ii][jj]=d[ii][jj];
else c[ii][jj]=c[i][j];
}
else
if (c[i][j]>c[ii][jj] && d[ii][jj]>=c[i][j])
c[ii][jj]=c[i][j];
}
}
t=st;
st=st->next;
delete t;
}
}
int main ()
{
read ();
distante ();
drum ();
ofstream fout ("barbar.out");
fout<<c[ifin][jfin];
return 0;
}