Pagini recente » Cod sursa (job #2078163) | Cod sursa (job #1664849) | Cod sursa (job #2904141) | Cod sursa (job #2814945) | Cod sursa (job #1292490)
#include<fstream>
using namespace std;
int n, k, LI, CI, LF, CF, f;
const int MAX = 500;
const int MAX_M = 55;
int lines[MAX];
int columns[MAX];
int head, tail;
int m[MAX_M][MAX_M];
int lee(int lineSource, int colSource, int lineDest, int colDest)
{
head = 0;
tail = 0;
lines[tail] = lineSource;
columns[tail] = colSource;
tail++;
int dispLine[] = { -1, 0, 1, 0 };
int dispCol[] = { 0, 1, 0, -1 };
while (head != tail)
{
int currentLine = lines[head];
int currentCol = columns[head];
head++;
for (int i = 0; i < 4; ++i)
{
int ii = currentLine + dispLine[i];
int jj = currentCol + dispCol[i];
if (ii >= 0 && ii < n)
{
if (jj >= 0 && jj < f)
{
m[ii][jj] = m[currentLine][currentCol] + 1;
lines[tail] = ii;
columns[tail] = jj;
tail++;
}
}
}
if (currentLine == lineDest && currentCol == colDest)
{
return m[currentLine][currentCol];
}
}
return -1;
}
int main()
{
ifstream cin("kdrum.in");
ofstream cout("kdrum.out");
cin >> n >> f >> k;
cin >> LI >> CI >> LF >> CF;
for(int i = 0; i < n; i++)
for(int j = 0; j < f; j++)
cin >> m[i][j];
cout << lee(LI - 1, CI - 1, LF - 1, CF - 1);
}