Pagini recente » Cod sursa (job #2102089) | Cod sursa (job #302052) | Cod sursa (job #865044) | Cod sursa (job #1499531) | Cod sursa (job #2113090)
#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];
// a[i][j]=gcd(a[i][j],k);
}
}
int lee()
{
queue <nod> q;
val.x=x1,val.y=x2;
q.push({x1,y1,1});
nod c1,c2;
while(!q.empty()&&mat[x2][y2]==0)
{
c1=q.front();
q.pop();
for ( k = 0 ; k <= 3 ; k ++ )
{
c2=c1;
c2.x=c2.x+dx[k];
c2.y=c2.y+dy[k];
c2.val_ind++;
if(!mat[c2.x][c2.y]&&a[c2.x][c2.y]!=0)
{q.push(c2);
mat[c2.x][c2.y]=mat[c1.x][c2.y]+1;
}
}
}
return c2.val_ind;
}
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 << n+m-1;
else
{
g <<lee();
}
return 0;
}