Cod sursa(job #1292479)

Utilizator GrandmasterSoucup Bogdan Grandmaster Data 14 decembrie 2014 13:26:53
Problema Kdrum Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.21 kb
#include<fstream>
using namespace std;
int n, k, LI, CI, LF, CF, f;
const int MAX = 100000;
const int MAX_M = 51;
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);
}