Cod sursa(job #1659331)

Utilizator alexionpopescuPopescu Ion Alexandru alexionpopescu Data 22 martie 2016 10:19:22
Problema Kdrum Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.71 kb
#include <fstream>
using namespace std;
ifstream fin("kdrum.in");
ofstream fout("kdrum.out");
int dl[4]={-1,0,1,0};
int dc[4]={0,1,0,-1};
struct drum{
    int l,c,d;
};
drum c[3000];
int a[101][101],b[101][101],k,divizor[1001],m,n,xs,ys,xf,yf,l;
void cit(){
    int i,j;
    fin>>m>>n>>k;
    fin>>xs>>ys>>xf>>yf;
    for(i=1;i<=m;i++)
        for(j=1;j<=n;j++)
            fin>>a[i][j];
    fin.close();
}
int cauta(int i,int j,int y){
    int m;
    if(i>j)
        return 0;
    m=(i+j)/2;
    if(divizor[m]==y)
        return m;
    if(y<divizor[m])
        return cauta(i,m-1,y);
    return cauta(m+1,j,y);
}
void cautare(){
    int i;
    divizor[1]=1;l=1;
    for(i=2;i<=k/2;i++)
        if(k%i==0)
            divizor[++l]=i;
    l++;
    divizor[l]=k;
}
void bord(){
    int i,j;
    for(i=1;i<=m;i++)
        for(j=1;j<=n;j++)
            if(cauta(1,l,a[i][j]))
                a[i][j]=0;
            else
                a[i][j]=1;
}
void lee(int xs,int ys){
    int in,jn,i,j,dist,dn,h,p,u;
    p=u=1;c[p].l=xs;c[p].c=ys;c[p].d=0;
    for(i=1;i<=m;i++)
        for(j=1;j<=n;j++)
            b[i][j]=-1;
    b[xs][ys]=0;
    while(p<=u){
        i=c[p].l;
        j=c[p].c;
        dist=c[p].d;
        for(h=0;h<4;h++){
            in=i+dl[h];
            jn=j+dc[h];
            dn=dist+1;
            if(in>0&&jn>0&&in<m+1&&jn<n+1&&b[in][jn]==-1){
                u++;
                c[u].l=in;
                c[u].c=jn;
                c[u].d=dn;
                b[in][jn]=dn;
            }
        }
        p++;
    }
}
int main(){
    cit();
    cautare();
    bord();
    lee(xs,ys);
    fout<<b[xf][yf]+1;
    fout.close();
    return 0;
}