Cod sursa(job #2113449)

Utilizator Anastasia11Susciuc Anastasia Anastasia11 Data 24 ianuarie 2018 16:26:50
Problema Kdrum Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.47 kb
#include <fstream>
#include <queue>

using namespace std;

ifstream f("kdrum.in");
ofstream g("kdrum.out");

int n,m,k,a[55][55],mat[55][55],i,j,x1,x2,y1,y2;
int ord[75],v[75];
struct nod{
int x, y,val_ind;
};
nod val;
int dx[]={0,0,-1,1};
int dy[]={1,-1,0,0};

int gcd(int a, int b)
{
    int r=1;
     while(b!=0)
     {
         r=a%b;
         a=b;
         b=r;
     }
    return a;
}
void read()
{
    f >> n >> m >> k;

    f >> x1 >> y1 >> x2 >> y2 ;
     for ( i = 1 ; i <= n ; i ++ )
       for( j = 1 ; j <= m ; j ++ )
          {f >> a[i][j];
            if (a[i][j]==0)
             mat[i][j]=-1;
           // a[i][j]=gcd(a[i][j],k);
          }

}

int lee()
{
    queue <nod> q;
    nod pr,nou;
       q.push({x1,y1,1});
    mat[x1][y1]=1;
     while(!q.empty()&&mat[x2][y2]==0)
     {
         pr=q.front();
         q.pop();
          for ( k = 0 ; k <= 3 ; k ++ )
          if(mat[pr.x+dx[k]][pr.y+dy[k]]==0)
           {
               nou=pr;
               nou.x+=dx[k];
               nou.y+=dy[k];
                q.push(nou);
                mat[pr.x+dx[k]][pr.y+dy[k]]=mat[pr.x][pr.y]+1;
           }

     }
    return mat[x2][y2];

}
void bord()
{
    for ( i = 0 ; i <= n + 1 ; i ++ )
      mat[i][0]=mat[i][m+1]=-1;
    for ( j = 0 ; j <= m + 1 ; j ++ )
      mat[0][j]=mat[n+1][j]=-1;

}
int main()
{
    read();
    bord();
    if ( k == 1 )
      g << lee();
     // else

    return 0;
}