Pagini recente » Cod sursa (job #1629063) | Cod sursa (job #855695) | Cod sursa (job #1448374) | Cod sursa (job #2660624) | Cod sursa (job #2110199)
#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];
}
bool viz[180][180];
int lee2(int Y1, int X1, int Y2, int X2) {
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] && nr[nou.y][nou.x]!=0) {
q.push(nou);
viz[nou.y][nou.x] = 1;
}
}
}
}
int main()
{
citire();
g << lee2(y1, x1, y2, x2)+1;
return 0;
}