Pagini recente » Cod sursa (job #1805113) | Cod sursa (job #1145010) | Cod sursa (job #1768851) | Cod sursa (job #1603919) | Cod sursa (job #2455482)
#include <iostream>
#include <fstream>
#include <queue>
using namespace std;
ifstream f ("car.in");
ofstream g ("car.out");
int n,m;
int startx, starty, stopx, stopy;
int mak[501][501];
int dir[501][501];
int di[] = {-1, -1, -1, 0, 1, 1, 1, 0};
int dj[] = {-1, 0, 1, 1, 1, 0, -1, -1};
struct segm{
int x1,y1,x2,y2;
};
queue < segm > coada;
queue < pair <int , int> > coada2;
bool isok(int x, int y)
{
if(1<=x && x<=n && 1<=y && y<=m)
return true;
return false;
}
void lee()
{
segm segmcurrent, segmnext;
segmcurrent.x1=startx;
segmcurrent.x2=startx;
segmcurrent.y1=starty;
segmcurrent.y2=starty;
mak[startx][startx] = 1;
dir[startx][starty] = 0;
coada.push(segmcurrent);
//coada.push2(make_pair(startx,starty));
int icurent = startx;
int jcurent = starty;
int inext, jnext;
while(!coada.empty())
{
segmcurrent=coada.front();
icurent = coada.front().x2;
jcurent = coada.front().y2;
coada.pop();
for(int ddir=0;ddir<8;++ddir)
{
// cout<<"j";
inext = icurent+di[ddir];
jnext = jcurent+dj[ddir];
// cout<<inext<<" "<<jnext<<" --"<<dir<<"\n";
if(isok(inext, jnext) && mak[inext][jnext]==0)
{
// cout<<"o";
int turn=0;
if(segmcurrent.x1==segmcurrent.x2 && segmcurrent.y1==segmcurrent.y2)
{
// cout<<"1";
mak[inext][jnext] = mak[icurent][jcurent]+0;
} else if((segmcurrent.x2 - inext)*(segmcurrent.y2 - segmcurrent.y1)== (segmcurrent.y2 - jnext)*(segmcurrent.x2 - segmcurrent.x1))
{
mak[inext][jnext] = mak[icurent][jcurent]+0;
} else if((segmcurrent.x2 - inext)*(segmcurrent.y2 - segmcurrent.y1)== -(segmcurrent.y2 - jnext)*(segmcurrent.x2 - segmcurrent.x1))
{
mak[inext][jnext] = mak[icurent][jcurent]+2;
} else if(inext == segmcurrent.x1 || jnext == segmcurrent.y1)
{
mak[inext][jnext] = mak[icurent][jcurent]+3;
} else {
mak[inext][jnext] = mak[icurent][jcurent]+1;
}
segm newseg;
newseg.x1 = segmcurrent.x2;
newseg.y1=segmcurrent.y2;
newseg.x2=inext;
newseg.y2=jnext;
coada.push(newseg);
}
}
}
}
int main()
{
f>>n>>m>>startx>>starty>>stopx>>stopy;
for(int i=1;i<=n;++i)
{
for(int j=1;j<=m;++j)
{
f>>mak[i][j];
if(mak[i][j]==1)
mak[i][j]=-1;
}
}
lee();
g<<mak[stopx][stopy]-1;
return 0;
}