Sunteți pe pagina 1din 2

#include <fstream> using namespace std; struct poz { int x, y; }; int main() { ifstream in("lee.in"); ofstream out("lee.

out"); int n, m; in >> n >> m; int a[n + 2][m + 2]; // START citire for(int i = 0; i < n + 1; i++) { for(int j = 0; j < m + 1; j++) { if(i != 0 && j != 0) in >> a[i][j]; else if(i == 0) a[0][j] = a[n + 1][j] = -1; else if(j == 0) a[i][0] = a[i][m + 1] = -1; } } a[n + 1][m + 1] = -1; poz start, stop; in >> start.y >> start.x >> stop.y >> stop.x; in.close(); // STOP citire // START lee int dirX[] = {0, -1, 0, 1}; int dirY[] = {1, 0, -1, 0}; int prim = 0, ultim = 0; poz coada[n * m]; coada[prim] = start; a[start.y][start.x] = 1; while(a[stop.y][stop.x] == 0 && prim <= ultim) { poz pozcur = coada[prim]; prim++; for(int i = 0; i < 4; i++) { poz pozurm; pozurm.y = pozcur.y + dirY[i]; pozurm.x = pozcur.x + dirX[i]; if(a[pozurm.y][pozurm.x] == 0) { a[pozurm.y][pozurm.x] = a[pozcur.y][pozcur.x] + 1; ultim++; coada[ultim] = pozurm; } } } out << a[stop.y][stop.x];

return 0; }

S-ar putea să vă placă și