Pagini recente » Cod sursa (job #2832021) | Cod sursa (job #1682554) | Cod sursa (job #2636811) | Cod sursa (job #1195437) | Cod sursa (job #2677774)
#include <fstream>
#include<queue>
using namespace std;
ifstream fin("barbar.in");
ofstream fout("barbar.out");
int d[1005][1005],b[1005][1005];
int n,m,i1,i2,j1,j2,maxx;
struct elem
{
int l,c;
}poz,poz1;
queue<elem>q;
int dl[]={0,1,0,-1},dc[]={1,0,-1,0};
bool interior(int i,int j)
{
if(i<1||i>n)
return 0;
if(j<1||j>m)
return 0;
return 1;
}
int main()
{
char c;
fin>>n>>m;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
fin>>c;
if(c=='*')
{
d[i][j]=b[i][j]=-1;
}
else
if(c=='I')
{
i1=i;
j1=j;
}
else
if(c=='O')
{
i2=i;
j2=j;
}
else
if(c=='D')
{
q.push({i,j});
d[i][j]=1;
}
}
while(!q.empty())
{
poz=q.front();
q.pop();
for(int k=0;k<4;k++)
{
poz1.l=poz.l+dl[k];
poz1.c=poz.c+dc[k];
if(interior(poz1.l,poz1.c))
{
if(d[poz1.l][poz1.c]!=-1&&(d[poz1.l][poz1.c]==0||d[poz1.l][poz1.c]>d[poz.l][poz.c]+1))
{
d[poz1.l][poz1.c]=d[poz.l][poz.c]+1;
q.push(poz1);
}
}
}
}
/*for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
fout<<d[i][j]<<" ";
fout<<"\n";
}
fout<<"\n";*/
b[i1][j1]=d[i1][j1];
q.push({i1,j1});
while(!q.empty())
{
poz=q.front();
q.pop();
for(int k=0;k<4;k++)
{
poz1.l=poz.l+dl[k];
poz1.c=poz.c+dc[k];
maxx=min(b[poz.l][poz.c],d[poz1.l][poz1.c]);
if(interior(poz1.l,poz1.c))
{
if(b[poz1.l][poz1.c]!=-1&&b[poz1.l][poz1.c]<maxx)
{
b[poz1.l][poz1.c]=maxx;
q.push(poz1);
}
}
}
}
/* for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
fout<<b[i][j]<<" ";
fout<<"\n";
}
fout<<"\n";
*/
fout<<b[i2][j2]-1;
return 0;
}