Cod sursa(job #96106)
#include<fstream.h>
int n , dx[4]={-1,0,1,0}, dy[4]={0,-1,0,1}, a[100][100];
typedef struct
{
int x, y, d;
} punct;
punct v[100];
int main()
{
cin>>n;
int p, u, i, j, xx, yy;
for (i=1; i<=n; i++)
a[0][i]=a[n+1][i]=a[i][0]=a[i][n+1]=-3;
p=u=1;
v[p].x=v[p].y=v[p].d=1;
a[1][1]=1;
while (p<=u)
{
for (i=0; i<4; i++)
{
xx=v[p].x+dx[i];
yy=v[p].y+dy[i];
if (a[xx][yy]!=-3)
{
a[xx][yy]=a[v[p].x][v[p].y]+1;
u++;
if (v[u].d!=1)
{
v[u].x=xx;
v[u].y=yy;
}
v[p].d=1;
}
}
p++;
}
ofstream out("test.out");
for (i=1; i<=n; i++)
{
for (j=1; j<=n; j++)
out<<a[i][j]<<" ";
out<<'\n';
}
out.close();
return 0;
}