Pagini recente » Cod sursa (job #170798) | Cod sursa (job #832398) | Cod sursa (job #444375) | Cod sursa (job #2191131) | Cod sursa (job #2653491)
#include <fstream>
#include <queue>
#include <iostream>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#include <stdio.h>
#include <ctype.h>
using namespace std;
ofstream fout("kdrum.out");
class InParser {
private:
FILE *fin;
char *buff;
int sp;
char read_ch() {
++sp;
if (sp ==1000000) {
sp = 0;
fread(buff, 1,1000000 , fin);
}
return buff[sp];
}
public:
InParser(const char* nume) {
fin = fopen(nume, "r");
buff = new char[4096]();
sp = 4095;
}
InParser& operator >> (int &n) {
char c;
while (!isdigit(c = read_ch()) && c != '-');
int sgn = 1;
if (c == '-') {
n = 0;
sgn = -1;
} else {
n = c - '0';
}
while (isdigit(c = read_ch())) {
n = 10 * n + c - '0';
}
n *= sgn;
return *this;
}
InParser& operator >> (long long &n) {
char c;
n = 0;
while (!isdigit(c = read_ch()) && c != '-');
long long sgn = 1;
if (c == '-') {
n = 0;
sgn = -1;
} else {
n = c - '0';
}
while (isdigit(c = read_ch())) {
n = 10 * n + c - '0';
}
n *= sgn;
return *this;
}
};
InParser fin("kdrum.in");
struct idk{
int i,j,mod;
};
const int vx[]={0,1,0,-1},vy[]={1,0,-1,0};
int n,m,k,x1,y1,x2,y2,v[55][55],dp[55][55][12005];
queue <idk> q;
int main()
{
fin>>n>>m>>k>>x1>>y1>>x2>>y2;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
fin>>v[i][j];
}
}
q.push({x1,y1,v[x1][y1]%k});
dp[x1][y1][v[x1][y1]%k] = 1;
while(!q.empty()){
int i=q.front().i;
int j=q.front().j;
int poz1=q.front().mod;
// cout << i << " " <<j << " " << poz1 << "\n";
q.pop();
for (int w=0;w<4;w++) {
int xx = i + vx[w];
int yy = j + vy[w];
if (v[xx][yy] != 0) {
int poz2=(v[xx][yy]%k*poz1)%k;
if (dp[xx][yy][poz2] == 0) {
dp[xx][yy][poz2]=dp[i][j][poz1] + 1;
q.push({xx, yy, poz2});
}
}
}
}
fout<<dp[x2][y2][0];
return 0;
}
///dp[xx][yy][cat e modulo cu drumul pana in xx,yy]= dp[i][j][modulo pana la i,j]