Pagini recente » Cod sursa (job #3278030) | Cod sursa (job #2041862) | Cod sursa (job #2265237) | Cod sursa (job #2075285) | Cod sursa (job #2532250)
#include <bits/stdc++.h>
#define PII pair<int,int>
#define TIII tuple <int,int,int>
#define Lin first
#define Col second
using namespace std;
ifstream in("barbar.in");
ofstream out("barbar.out");
int n,m;
int dist[1002][1002];
bool viz[1002][1002];
queue <PII> c;
priority_queue <TIII> Max;
const short int dLin[]={-1,0,1,0},dCol[]={0,1,0,-1};
int iSt,jSt,iFin,jFin;
void citire()
{
in>>n>>m;
for(int i=0;i<=n+1;i++)
viz[i][0]=viz[i][m+1]=1;
for(int j=0;j<=m+1;j++)
viz[0][j]=viz[n+1][j]=1;
char cr;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
in>>cr;
switch(cr)
{
case '.':
break;
case '*':
viz[i][j]=1;
break;
case 'D':
viz[i][j]=1;
c.push({i,j});
break;
case 'I':
iSt=i;jSt=j;
break;
case 'O':
iFin=i;jFin=j;
break;
}
}
}
int main()
{
citire();
while(!c.empty())
{
int i=c.front().Lin,j=c.front().Col;
c.pop();
for(int ip,jp,k=0;k<=3;k++)
if(!viz[ i+dLin[k] ][ j+dCol[k] ])
{
ip=i+dLin[k];
jp=j+dCol[k];
dist[ip][jp]=dist[i][j]+1;
viz[ip][jp]=1;
c.push({ip,jp});
}
}
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
if(dist[i][j])
viz[i][j]=0;
viz[iSt][jSt]=1;
Max.push( make_tuple( dist[iSt][jSt], iSt, jSt ) );
while(!Max.empty() && !viz[iFin][jFin])
{
int noth,i,j;
tie(noth,i,j)=Max.top();
Max.pop();
for(int ip,jp,k=0;k<=3;k++)
if(!viz[ i+dLin[k] ][ j+dCol[k] ])
{
ip=i+dLin[k];
jp=j+dCol[k];
dist[ip][jp]=min(dist[ip][jp],dist[i][j]);
viz[ip][jp]=1;
Max.push( make_tuple( dist[ip][jp] , ip, jp ) );
}
}
if(!viz[iFin][jFin])
out<<-1;
else
out<<dist[iFin][jFin];
return 0;
}