Cod sursa(job #2110199)

Utilizator SqueekDanielTodasca Daniel SqueekDaniel Data 20 ianuarie 2018 12:59:09
Problema Kdrum Scor 20
Compilator cpp Status done
Runda evaluare_cex_sv_cls_x_2 Marime 1.14 kb
#include <iostream>
#include <fstream>
#include <queue>
std::ifstream f("kdrum.in");
std::ofstream g("kdrum.out");

int nr[55][55];
int N, M, K;
int y1, x1, y2, x2;
void citire() {
    f >> N >> M >> K;
    f >> y1 >> x1 >> y2 >> x2;
    for (int i=1, j; i<=N; i++)
        for (j=1; j<=M; j++)
            f >> nr[i][j];
}


bool viz[180][180];
int lee2(int Y1, int X1, int Y2, int X2) {
    struct nod {
    int y, x;
    int p=0;
    };
    int dx[] = {1, -1, 0, 0};
    int dy[] = {0, 0, 1, -1};
    std::queue <nod> q;
    nod nd;
    nd.y = Y1;
    nd.x = X1;
    q.push(nd);
    nod nou, pres;

    while(!q.empty()) {
        pres = q.front();
        q.pop();

        if (pres.x == X2 && pres.y == Y2) {
            return pres.p;
        }

        for (int i=0; i<4; i++) {
            nou = pres; nou.p++;
            nou.x += dx[i]; nou.y += dy[i];
            if (!viz[nou.y][nou.x] && nr[nou.y][nou.x]!=0) {
                q.push(nou);
                viz[nou.y][nou.x] = 1;
            }
        }
    }
}


int main()
{
    citire();
    g << lee2(y1, x1, y2, x2)+1;

    return 0;
}