Cod sursa(job #3042268)

Utilizator pluca82popa Luca pluca82 Data 5 aprilie 2023 11:04:45
Problema Rj Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.26 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("rj.in");
ofstream fout("rj.out");

struct coord{
    int x, y;
};

queue <coord> q;

short a[500][500], b[500][500];
short dx[] = {1, -1, 0, 0};
short dy[] = {0, 0, 1, -1};
int n, m, xs, ys, xf, yf;

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

void bordare(){
    for(int i = 0; i <= n + 1; i++){
        a[i][0] = a[i][n + 1] = 1;
    }
    for(int i = 0; i <= n + 1; i++){
        a[0][i] = a[n + 1][i] = 1;
    }
}

bool ok(int i, int j){
    return i > 0 && j > 0 && i <= n && j <= n;
}

void lee(){
    coord w, w1;
    w.x = xs;
    w.y = ys;
    q.push(w);
    b[xs][ys] = 1;
    while(!q.empty()){
        w = q.front();
        q.pop();

        for(int i = 0; i <= 3; i++){
            w1.x = w.x + dx[i];
            w1.y = w.y + dy[i];
            if(ok(w1.x, w1.y) && a[w1.x][w1.y] == 0 && (b[w1.x][w1.y] == 0 || b[w1.x][w1.y] > b[w.x][w.y] + 1)){
                b[w1.x][w1.y] = b[w.x][w.y] + 1;
                q.push(w1);
            }
        }
    }
}

void afisare(){
    fout << b[xf][yf] << "\n";
}

int main()
{
    citire();
    bordare();
    lee();
    afisare();
    return 0;
}