Pagini recente » Cod sursa (job #2770400) | Cod sursa (job #2391798) | Cod sursa (job #265138) | Cod sursa (job #1505735) | Cod sursa (job #2110092)
#include <iostream>
#include <fstream>
#include <queue>
std::ifstream f("kdrum.in");
std::ofstream g("kdrum.out");
int nr[55][55];
int N, M, K;
int y1, x1, y2, x2;
void citire() {
f >> N >> M >> K;
f >> y1 >> x1 >> y2 >> x2;
for (int i=1, j; i<=N; i++)
for (j=1; j<=M; j++)
f >> nr[i][j];
}
int pasi_max = 15;
struct nod {
int y, x, pasi=0, left=K;
};
int gcd(int a, int b) { //dinamic?
if(a==0) return b;
if(b==0) return a;
return gcd(b, a%b);
} void calcul(nod &nd, int x) {
nd.left = nd.left/gcd(nd.left, x);
}
int lee2(int Y1, int X1, int Y2, int X2) {
bool viz[180][180];
for (int i=0, j; i<180; i++)
for (j=0; j<180; j++)
viz[i][j] = 0;
for (int i=0; i<=N+1; i++)
viz[0][i] = viz[i][0] = viz[N+1][i] = viz [i][N+1] = 1;
struct nod {
int y, x;
int p=0;
};
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
std::queue <nod> q;
nod nd;
nd.y = Y1;
nd.x = X1;
q.push(nd);
nod nou, pres;
while(!q.empty()) {
pres = q.front();
q.pop();
if (pres.x == X2 && pres.y == Y2) {
return pres.p;
}
for (int i=0; i<4; i++) {
nou = pres; nou.p++;
nou.x += dx[i]; nou.y += dy[i];
if (!viz[nou.y][nou.x]) {
q.push(nou);
viz[nou.y][nou.x] = 1;
}
}
}
}
int main()
{
citire();
g << lee2(y1, x1, y2, x2)+1;
return 0;
}