Pagini recente » Cod sursa (job #313022) | Cod sursa (job #1186045) | Cod sursa (job #889199) | Cod sursa (job #3160450) | Cod sursa (job #1822328)
#include <iostream>
#include <fstream>
#include <queue>
#include <cmath>
#define N 145
using namespace std;
ifstream f("jaina.in");
ofstream g("jaina.out");
int i,j,n,m,k;
struct gogu
{
int x,y;
}plec,sos,el,vecin;
struct elev
{
int val,poz;
}a[N][N];
queue<pair<int,int> > cod;
int d1[8]={0,-1,0,1,-1,1,-1,1};
int d2[8]={-1,0,1,0,-1,-1,1,1};
inline void lee()
{
int p1,p2;
cod.push(make_pair(plec.x,plec.y));
while(!cod.empty()){
el.x=cod.front().first;
el.y=cod.front().second;
cod.pop();
for(int ind=0;ind<8;ind++)
{
vecin.x=el.x+d1[ind];
vecin.y=el.y+d2[ind];
if(a[vecin.x][vecin.y].val==0)
{
a[vecin.x][vecin.y].val=a[el.x][el.y].val+1;
cod.push(make_pair(vecin.x,vecin.y));
}
else
if(a[vecin.x][vecin.y].val<0)
{
p1=(-1)*(a[vecin.x][vecin.y].val);
p2=a[vecin.x][vecin.y].poz;
if(a[p1][p2].val==0)
{
a[p1][p2].val=a[el.x][el.y].val+1;
cod.push(make_pair(p1,p2));
}
}
}
}
}
int main()
{
int x1,y1;
f>>n;
f>>plec.x>>plec.y>>sos.x>>sos.y;
f>>k;
for(i=1;i<=k;i++)
{
f>>x1>>y1;
if(a[x1][y1].val==0)
{
for(j=1;j<=n;j++)
{
a[x1][j].val=-1*(x1);
a[j][y1].val=-1*(x1);
a[x1][j].poz=y1;
a[j][y1].poz=y1;
}
}
else
if(abs(a[x1][y1].val)>x1)
{
for(j=1;j<=n;j++)
{
a[x1][j].val=-1*(x1);
a[j][y1].val=-1*(x1);
a[x1][j].poz=y1;
a[j][y1].poz=y1;
}
}
a[x1][y1].val=0;
}
for(i=0;i<=n+1;i++)
{
a[0][i].val=2;
a[n+1][i].val=2;
a[i][0].val=2;
a[i][n+1].val=2;
}
lee();
g<<a[sos.x][sos.y].val;
}