Pagini recente » Cod sursa (job #1565223) | Profil hominidu | Cod sursa (job #2263508) | Cod sursa (job #1539967) | Cod sursa (job #465053)
Cod sursa(job #465053)
#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();
}
ofstream fout("kdrum.out");
void solve()
{x.push(x1);
y.push(yi1);
d.push(1);
long aux,i,r,j,k,ll,cc,dd,d1,pl,pr;
for(i=2;i<=250;++i)
if(a[x1][yi1]%i==0)
while(kk%i==0&&kk>1)
kk/=i;
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)
{ for(r=2;r<=250;++r)
if(a[i][j]%r==0)
while(pr%r==0&&pr>1)
pr/=r;
}
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;
}