Cod sursa(job #264152)

Utilizator AndreiDDiaconeasa Andrei AndreiD Data 21 februarie 2009 16:15:19
Problema Kdrum Scor 20
Compilator c Status done
Runda Arhiva de probleme Marime 1.71 kb
#include <stdio.h>
#include <string.h>
#include <math.h>

#define Nmax 70

int dl[]={0,1,0,-1};
int dc[]={-1,0,1,0};
int N,M,K;
int X1,Y1,X2,Y2;
int i,j,lne,cne;
int D[Nmax][Nmax];
int ql[7*Nmax],qc[7*Nmax];
int lee1,lee2,x,k;
int l,c;
char A[Nmax][Nmax];

void read_data()
{
    freopen("kdrum.in","r",stdin);
    freopen("kdrum.out","w",stdout);
    
    scanf("%d %d %d", &N,&M,&K);
    scanf("%d %d %d %d", &X1,&Y1,&X2,&Y2);
    for (i=0;i<N;++i)
          for (j=0;j<M;++j)
               {
                   scanf("%d",&x);
                   if (x!=0)
                        A[i][j]='.';
                        else
                        A[i][j]='X'; //nu poate trece
                   D[i][j]=666666;   
     
               }  
          X1--;
          X2--;
          Y1--;
          Y2--;     
}

int posibil(int X, int Y)
{
    return (X>=0 && X<=N && Y>=0 && Y<=M);
}   


void lee()
{
    lee1=1;   
    ql[1]=X1;   
    qc[1]=Y1;   
    D[X1][Y1]=0;   
    ql[2]=X2;   
    qc[2]=Y2;   
    lee2=3;   
  
    while (lee1<lee2)   
    {   
    l=ql[lee1];   
    c=qc[lee1];   
    for (k=0;k<4;++k)   
        {   
        lne=l+dl[k];   
        cne=c+dc[k];   
        if (posibil(lne,cne))   
            {   
             if (D[lne][cne]>D[l][c]+1 && A[lne][cne]!='X')   
                {   
                D[lne][cne]=D[l][c]+1;   
                ql[lee2]=lne;   
                qc[lee2++]=cne;   
                }   
            }   
        }   
        lee1++;   
    }   
}   


void solve()
{
    lee();
    if (K==1)
         printf("%d\n", D[X2][Y2]+1);
}
    
int main()
{
    read_data();
    solve();
    return 0;
}