1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
|
#include<iostream>
#include<cstdio>
#include<conio.h>
#include<algorithm>
using namespace std;
void pushNeighbours(int);
void pushCrtLvl(int, int);
int mat[5][5],crtLvl[25][2],nxtLvl[25][2],top=0,top1=0,level=0;
using namespace std;
int main()
{
int i,j,count=0;
for(i=0;i<5;i++)
fill_n(*(mat+i),5,255);
crtLvl[0][0]=crtLvl[0][1]=2;
top++;
while(top!=0)
{
if(mat[crtLvl[top-1][0]][crtLvl[top-1][1]]==255)
{
mat[crtLvl[top-1][0]][crtLvl[top-1][1]]=level;
count++;
pushNeighbours(top-1);
}
top-=1;
if(top==0)
{
level++;
top=top1;
for(i=0;i<top1;i++)
copy(nxtLvl[i],nxtLvl[i]+2,crtLvl[i]);
top1=0;
for(i=0;i<25;i++)
fill_n(*(nxtLvl+i),2,0);
}
if(count==25)
{
break;
}
}
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
cout<<mat[i][j]<<" ";
cout<<'\n';
}
getch();
}
void pushNeighbours(int crt)
{
pushCrtLvl(crtLvl[crt][0],(crtLvl[crt][1]+1));
pushCrtLvl((crtLvl[crt][0]+1),crtLvl[crt][1]);
pushCrtLvl(crtLvl[crt][0],(crtLvl[crt][1]-1));
pushCrtLvl((crtLvl[crt][0]-1),crtLvl[crt][1]);
}
void pushCrtLvl(int r,int c)
{
if((r<5 & r>=0)&(c<5 & c>=0))
{
nxtLvl[top1][0]=r;
nxtLvl[top1][1]=c;
++top1;
}
}
|