Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <queue>
- using namespace std;
- ifstream fin("alee.in");
- ofstream fout("alee.out");
- int di[4] = {0, 0, 1, -1};
- int dj[4] = {1, -1, 0, 1};
- int N, M, Map[180][180];
- int startx, starty, stopx, stopy;
- queue < pair < int, int > > coada;
- void Citire()
- {
- fin >> N >> M;
- for(int i = 1; i <= M; i++)
- {
- int x, y;
- fin >> x >> y;
- Map[x][y] = -1; //Map[x][y] e obstacol
- }
- fin >> startx >> starty;
- fin >> stopx >> stopy;
- }
- bool OK(int i, int j)
- {
- if(i < 1 || j < 1 || i > N || j > N) // Daca depaseste granitele
- return false;
- if(Map[i][j] == -1) // Daca intalneste un obstacol
- return false;
- return true;
- }
- void Lee()
- {
- int i, j, i_urmator, j_urmator;
- Map[startx][starty] = 1;
- coada.push(make_pair(startx, starty)); //Inserare pozitie de pornire in coada
- while(!coada.empty()) // Verifica daca coada este libera
- {
- i = coada.front().first; // Extrage coordonata x a primului punct din coada
- j = coada.front().second; // Extrage coordonata y a primului punct din coada
- coada.pop();
- for(int dir = 0; dir < 4; dir++)
- {
- i_urmator = i + di[dir];
- j_urmator = j + dj[dir];
- if(OK(i_urmator, j_urmator) && Map[i_urmator][j_urmator] < 1)
- {
- Map[i_urmator][j_urmator] = Map[i][j] + 1;
- coada.push(make_pair(i_urmator, j_urmator));
- }
- }
- }
- }
- int main()
- {
- Citire();
- Lee();
- fout << Map[stopx][stopy];
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement