imagine-fundal

PBinfo.ro / Roboti / 864 facebook instagram whatsapp tiktok twitter

#include <iostream>
#include <fstream>
using namespace std;
ifstream f("roboti.in");
ofstream g("roboti.out");
int i, j, x1, y1, ok, n, m, x, y, u, a[1002][1002];
int xr, yr,dx[]= {-1, 0, 1, 0}, dy[]= {0, 1, 0, -1};
struct coada { int x, y, a; } q[1000004];
void lee(int xr, int yr, int x1, int y1, int &ok, int &u) {
    int p, xx, yy;
    p = u = 1;
    ok = 0;
    q[1].x = xr;
    q[1].y = yr;
    q[p].a = 0;
    a[xr][yr] = -1;
    while (p <= u && ok == 0) {
        for (i = 0; i <= 3; i++) {
            xx = q[p].x + dx[i];
            yy = q[p].y + dy[i];
            if (a[xx][yy] == 0) {
                u++;
                q[u].x = xx;
                q[u].y = yy;
                q[u].a = q[p].a + 1;
                a[xx][yy] = -1;
                if(q[u].x == x1 && q[u].y == y1) ok = 1;
            }
        } p++;
    }
}
int main() {
    f >> n >> m;
    for (i = 1; i <= n; i++)
        for (j = 1; j <= m; j++)
        	f >> a[i][j];
	f >> xr >> yr >> x1 >> y1;
    for (i = 1; i <= n; i++)
        a[i][0] = a[i][m + 1] = 1;
    for (i = 1; i <= m; i++)
        a[0][i] = a[n + 1][i] = 1;
    lee(xr, yr, x1, y1, ok, u);
    if (ok == 1) g << q[u].a;
    else g << -1;
    return 0;
}

Secțiune de comentarii prin GitHub Issues github logo

Am greșit ceva? Modifică rezolvarea mai jos! github logo