Cod sursa(job #2274805)

Utilizator memecoinMeme Coin memecoin Data 2 noiembrie 2018 15:32:23
Problema Kdrum Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.06 kb
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <math.h>
#include <queue>
 
using namespace std;

int n, m, k;
int si, sj, di, dj;

int deltaI[4] = { 1,-1,0,0 };
int deltaJ[4] = { 0,0,1,-1 };

int a[52][52];

int cmmdc(int a, int b) {
	if (b == 0) {
		return a;
	}
	return cmmdc(b, a % b);
}

struct Event {
	int i, j, k, cost;
};

queue<Event> q;

int main() {
	freopen("kdrum.in", "r", stdin);
	freopen("kdrum.out", "w", stdout);

	scanf("%d %d %d", &n, &m, &k);

	scanf("%d %d %d %d", &si, &sj, &di, &dj);

	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j <= m; ++j) {
			scanf("%d", &a[i][j]);
		}
	}
	
	q.push({ si, sj, k / cmmdc(k, a[si][sj]), 1 });

	while (true) {
		auto x = q.front();
		q.pop();

		if (x.i == di && x.j == dj && x.k == 1) {
			printf("%d", x.cost);
			return 0;
		}

		for (int d = 0; d < 4; ++d) {
			int di = x.i + deltaI[d];
			int dj = x.j + deltaJ[d];

			if (a[di][dj] != 0) {
				q.push({ di, dj, x.k / cmmdc(x.k, a[di][dj]), x.cost + 1 });
			}
		}
	}

	return 0;
}