Pagini recente » Cod sursa (job #826202) | Cod sursa (job #2629827) | Cod sursa (job #3288127) | Cod sursa (job #925788) | Cod sursa (job #2110103)
#include <bits/stdc++.h>
#define pp pair< bool, int >
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 ];
pp 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;
}