Pagini recente » Cod sursa (job #510110) | Cod sursa (job #146251) | Cod sursa (job #2935730) | Cod sursa (job #141669) | Cod sursa (job #3341413)
#include <fstream>
#include <queue>
using namespace std;
ifstream cin("barbar.in");
ofstream cout("barbar.out");
struct cow
{
int ox, oy, c;
};
queue <cow> q;
priority_queue <pair<int, pair<int, int>>> pq;
int viz[1005][1005];
int v[1005][1005];
int dx[5]={1, 0, 0, -1};
int dy[5]={0, 1, -1, 0};
int main()
{
char ch;
int n, m, x, y, cnt=0, sx, sy, fx, fy, nx, ny, mn=1000005;
cin>>n>>m;
for(int i=1; i<=n; i++)
{
viz[i][0]=viz[i][n+1]=-1;
for(int j=1; j<=m; j++)
{
viz[0][j]=viz[m+1][j]=-1;
cin>>ch;
if(ch=='D')
{
q.push({i, j, 0});
viz[i][j]=1;
}
else if(ch=='*')
{
viz[i][j]=-1;
}
else if(ch=='I')
{
sx=i;
sy=j;
}
else if(ch=='O')
{
fx=i;
fy=j;
}
}
}
while(!q.empty())
{
x=q.front().ox;
y=q.front().oy;
cnt=q.front().c;
v[x][y]=cnt;
q.pop();
for(int i=0; i<4; i++)
{
nx=dx[i]+x;
ny=dy[i]+y;
if(viz[nx][ny]==0)
{
viz[nx][ny]=1;
q.push({nx, ny, cnt+1});
}
}
}
pq.push({v[sx][sy],{sx, sy}});
while(!pq.empty())
{
x=pq.top().second.first;
y=pq.top().second.second;
cnt=pq.top().first;
pq.pop();
mn=min(mn, cnt);
if(x==fx && y==fy)
break;
for(int i=0; i<4; i++)
{
nx=x+dx[i];
ny=dy[i]+y;
if(viz[nx][ny]==1)
{
viz[nx][ny]=2;
pq.push({v[nx][ny], {nx, ny}});
}
}
}
cout<<mn;
return 0;
}