Cod sursa(job #2110178)

Utilizator CronosClausCarare Claudiu CronosClaus Data 20 ianuarie 2018 12:53:27
Problema Kdrum Scor 20
Compilator cpp Status done
Runda evaluare_cex_sv_cls_x_2 Marime 1.36 kb
#include <bits/stdc++.h>

using namespace std;

const int mxn = 50 + 10;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

int n, m, k, x2, y2;;
int a[ mxn ][ mxn ];
bool v[ mxn ][ mxn ];
int rest[ mxn ][ mxn ];
int c[ mxn ][ mxn ];

void lee(int x1, int y1){
    queue< int > xx, yy;
    xx.push( x1 );
    yy.push( y1 );
    v[ x1 ][ y1 ] = 1;
    c[ x1 ][ y1 ] = 1;
    int x, y, nx, ny;
    while(!xx.empty()){
        x = xx.front();
        y = yy.front();
        xx.pop();
        yy.pop();
        if(x == x2 && y == y2)
            xx = queue< int >();
        else{
            for(int i = 0; i < 4; i++){
                nx = x + dx[ i ];
                ny = y + dy[ i ];
                if(a[ nx ][ ny ] && v[ nx ][ ny ] == 0){
                    v[ nx ][ ny ] = 1;
                    c[ nx ][ ny ] = c[ x ][ y ] + 1;
                    xx.push( nx );
                    yy.push( ny );
                }
            }
        }
    }
}

int main()
{
    ios_base::sync_with_stdio( false );
    cin.tie( 0 );
    ifstream cin("kdrum.in");
    ofstream cout("kdrum.out");
    int x1, y1;
    cin>> n >> m >> k;
    cin>> x1 >> y1 >> x2 >> y2;
    for(int i = 1; i <= n; i++)
        for(int j = 1, aux; j <= m; j++){
            cin>> a[ i ][ j ];
        }
    lee(x1, y1);
    cout<< c[ x2 ][ y2 ];
    return 0;
}