Pagini recente » Cod sursa (job #536128) | Cod sursa (job #2567370) | Cod sursa (job #2123031) | Cod sursa (job #2322481) | Cod sursa (job #2159427)
#include <bits/stdc++.h>
using namespace std;
ifstream f("car.in");
ofstream g("car.out");
int n,m,x,y,X,Y,M[502][502];
bool viz[502][502];
int dx[]={-1,-1,-1,0,1,1,1,0};
int dy[]={-1,0,1,1,1,0,-1,-1};
int cc[]={0,1,2,3,4,3,2,1};
struct pt
{
int a,b,c;
};
queue <pt> Q[4];
int main()
{
f>>n>>m>>x>>y>>X>>Y;
for(int i=1;i<=n;++i)
for(int j=1;j<=m;++j) f>>M[i][j];
for(int k=0;k<8;++k) Q[0].push({x,y,k});
for(int i=0;i<=1e6;++i)
while(!Q[i&3].empty())
{
x=Q[i&3].front().a;
y=Q[i&3].front().b;
int dir=Q[i&3].front().c;
int cost=i;
if(x==X&&y==Y)
{
g<<i;
return 0;
}
Q[i&3].pop();
if(viz[x][y]) continue;
viz[x][y]=1;
for(int k=0;k<8;++k)
if(cc[k]<4)
{
int newdir=(dir+k)%8;
int xx=x+dx[newdir];
int yy=y+dy[newdir];
int costy=cost+cc[k];
if(!xx||!yy||xx>n||yy>m) continue;
if(M[xx][yy]) continue;
if(!viz[xx][yy])
{
//g<<x<<' '<<y<<' '<<xx<<' '<<yy<<'\n';
Q[costy&3].push({xx,yy,newdir});
}
}
}
g<<-1;
return 0;
}