Cod sursa(job #2494728)

Utilizator Alexandra16Alexandra Malutan Alexandra16 Data 18 noiembrie 2019 12:47:12
Problema Text Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.06 kb
#include <iostream>
#include <fstream>
#include <queue>
#include <memory.h>

using namespace std;

const int MAXN = 180;
const int MAXM = MAXN * MAXN;

const int dx[]={-1,1,0,0};
const int dy[]={0,0,-1,1};

ifstream fin("alee.in");
ofstream fout("alee.out");
int a[MAXN][MAXN];

int n, m, x, y, xf, yf, xs, ys;

queue <pair <int, int> > q;

void citire(){
	fin >> n;
	fin >> m;
	memset(a, -1, sizeof(a));
	for (int i=1; i<=m; i++){
		fin >> x >> y;
		a[x][y]= -2;
	}
	fin >> xs >> ys >> xf >> yf;
	a[xs][ys]= 1;
}

inline bool inside(int x, int y, int n){
	return 1<=x && x <=n && 1<=y && y<=n;
}

void solve(){
	q.push(make_pair(xs,ys));
	while(!q.empty()){
		pair<int, int> pos = q.front();
		q.pop();
		for(int d = 0; d < 4; d++){
			int newx= pos.first + dx[d];
			int newy= pos.second +dy[d];
			if(inside(newx, newy, n) && a[newx][newy]==-1){
				q.push(make_pair(newx,newy));
				a[newx][newy]=a[pos.first][pos.second]+1;
			}
		}
	}
}

void write(){
	fout << a[xf][yf];
}

int main(){citire();
  solve();
  write();

}