Cod sursa(job #2110109)

Utilizator CronosClausCarare Claudiu CronosClaus Data 20 ianuarie 2018 12:28:31
Problema Kdrum Scor 0
Compilator cpp Status done
Runda evaluare_cex_sv_cls_x_2 Marime 1.55 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;
int x1, y1, x2, y2;
int a[ mxn ][ mxn ];
pair< int, int > b[ mxn ][ mxn ];
int c[ mxn ][ mxn ];

void lee(){
    queue< int > xx, yy;
    xx.push( x1 );
    yy.push( y1 );
    if(a[ x1 ][ y1 ] % k == 0){
        b[ x1 ][ y1 ].first = 1;
        b[ x1 ][ y1 ].second = 0;
    }
    b[ x1 ][ y1 ].second = a[ x1 ][ y1 ] % k;
    c[ x1 ][ y1 ] = 1;
    int x, y, nx, ny;
    if(b[ x1 ][ y1 ].first == 0)
    while(!xx.empty()){
        x = xx.front();
        y = yy.front();
        xx.pop();
        yy.pop();
        for(int i = 0; i < 4; i++){
            nx = x + dx[ i ];
            ny = y + dy[ i ];
            if(a[ nx ][ ny ]){
                if(b[ nx ][ ny ].first == 0){
                    b[ nx ][ ny ].second = (b[ x ][ y ].second * a[ nx ][ ny ]) % k;
                    c[ nx ][ ny ] = c[ x ][ y ] + 1;
                    b[ nx ][ ny ].first = 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");
    cin>> n >> m >> k;
    cin>> x1 >> y1 >> x2 >> y2;
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= m; j++){
            cin>> a[ i ][ j ];
            b[ i ][ j ].second = 1;
        }
    lee();
    cout<< c[ x2 ][ y2 ];
    return 0;
}