Pagini recente » Cod sursa (job #2981192) | Cod sursa (job #152321) | Cod sursa (job #1659860) | Cod sursa (job #1884139) | Cod sursa (job #1900655)
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 50;
const int MAXK = 12000;
int st[MAXN + 2][MAXN + 2][200];
int mat[MAXN + 2][MAXN + 2];
int divi[MAXK + 1], divv[200];
int dirl[] = {-1, 0, 1, 0}, dirc[] = {0, 1, 0, -1};
inline int cmmdc(int a, int b) {
int r;
while (b) {
r = a % b; a = b; b = r;
}
return a;
}
struct Stuff {
int lin, col, dv;
};
queue <Stuff> q;
int main()
{
int n, m, k, x1, x2, y1, y2, nd, l, c, d, newl, newc, newd;
ifstream fin("kdrum.in");
fin >> n >> m >> k >> x1 >> y1 >> x2 >> y2;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j)
fin >> mat[i][j];
fin.close();
nd = 0;
for (d = 1; d <= k; ++d)
if (k % d == 0) {
divi[d] = ++nd;
divv[nd] = d;
}
st[x1][y1][divi[cmmdc(mat[x1][y1], k)]] = 1;
q.push({x1, y1, divi[cmmdc(mat[x1][y1], k)]});
while (q.empty() == 0) {
l = q.front().lin; c = q.front().col; d = q.front().dv;
for (int i = 0; i < 4; ++i) {
newl = l + dirl[i]; newc = c + dirc[i]; newd = divi[cmmdc(divv[d] * mat[newl][newc], k)];
if (mat[newl][newc] && st[newl][newc][newd] == 0) {
st[newl][newc][newd] = st[l][c][d] + 1;
q.push({newl, newc, newd});
}
}
q.pop();
}
ofstream fout("kdrum.out");
fout << st[x2][y2][nd] << '\n';
fout.close();
return 0;
}