Pagini recente » Cod sursa (job #2731171) | Cod sursa (job #1416161) | Cod sursa (job #1156553) | Cod sursa (job #2573055) | Cod sursa (job #2110124)
#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 a[ mxn ][ mxn ];
pair< int, int > b[ mxn ][ mxn ];
int c[ mxn ][ mxn ];
void lee(int x1, int y1){
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");
int x1, y1, x2, y2;
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(x1, y1);
cout<< c[ x2 ][ y2 ];
return 0;
}