Pagini recente » Cod sursa (job #681922) | Cod sursa (job #2975980) | Cod sursa (job #140005) | Cod sursa (job #1166142) | Cod sursa (job #2457471)
#include <fstream>
#include <queue>
using namespace std;
ifstream in ("kdrum.in");
ofstream out ("kdrum.out");
short dx[4] = {0,1,0,-1};
short dy[4] = {1,0,-1,0};
struct info
{
int dist,x,y,tatax,tatay,rez;
};
queue <info> q;
int a[51][51];
int n,m,k,x1,y1,x2,y2;
bool verf (int x,int y)
{
if(x>n || x<1 || y>m || y<1 ||a[x][y]==0)
return 0;
return 1;
}
void lee (int startx, int starty)
{
info start;
start.dist = a[startx][starty];
start.x=startx;
start.y=starty;
start.tatax = -1;
start.tatay = -1;
start.rez = 1;
q.push(start);
while (!q.empty())
{
info pct;
pct=q.front();
for (int i = 0;i<=3;++i)
{
int newx = pct.x+dx[i];
int newy = pct.y+dy[i];
if (verf(newx,newy) && !(pct.tatax==newx && pct.tatay==newy))
{
int newdist = ((pct.dist%k) * (a[newx][newy] %k))%k;
if (newdist == 0 && newx==x2 && newy==y2)
{
out<<pct.rez+1;
return;
}
else
q.push({newdist,newx,newy,newx,newy,pct.rez+1});
}
}
q.pop();
}
return ;
}
int main ()
{
in>>n>>m>>k>>x1>>y1>>x2>>y2;
for (int i = 1;i<=n;++i)
for(int j = 1;j<=m;++j)
in>>a[i][j];
lee(x1,y1);
}