Pagini recente » Cod sursa (job #2258683) | Cod sursa (job #2634786) | Cod sursa (job #2117546) | Cod sursa (job #1712977) | Cod sursa (job #465055)
Cod sursa(job #465055)
#include<fstream.h>
#include<queue>
using namespace std;
queue<long>x,y,d;
queue<long>p;
int dl[4]={-1,0,1,0};
int dc[4]={0,1,0,-1};
long a[55][55],n,m,kk,x1,yi1,x2,y2,b[55][55];
void cit()
{ifstream fin("kdrum.in");
fin>>n>>m>>kk>>x1>>yi1>>x2>>y2;
long i,j;
for(i=1;i<=n;++i)
for(j=1;j<=n;++j)
fin>>a[i][j];
fin.close();
}
long cmmdc(long a,long b)
{if(b!=0)
return cmmdc(b,a%b);
else
if(b==0)
return a;
}
ofstream fout("kdrum.out");
void solve()
{x.push(x1);
y.push(yi1);
d.push(1);
long cd,aux,i,r,j,k,ll,cc,dd,d1,pl,pr;
cd=cmmdc(kk,a[x1][yi1]);
kk/=cd;
p.push(kk);
while(!x.empty())
{ll=x.front();
cc=y.front();
dd=d.front();
pl=p.front();
for(k=0;k<=3;++k)
{i=ll+dl[k];
j=cc+dc[k];
d1=dd+1;
pr=pl;
if(a[i][j]&&b[i][j]==0)
{if(pr>1)
{cd=cmmdc(pr,a[i][j]);
pr/=cd;
}
if(pr==1)
b[i][j]=1;
x.push(i);
y.push(j);
d.push(d1);
p.push(pr);
if(i==x2&&j==y2&&pr==1)
{ fout<<d1<<'\n';
fout.close();
exit(0);
}
}
}
x.pop();
y.pop();
d.pop();
p.pop();
}
}
int main()
{cit();
solve();
return 0;
}