10 11 12 1
9 16 13 2
8 15 14 3
7 6 5 4
#include <stdio.h>
#include <stdlib.h>
#define MAX 16
//移动轨迹,下左上右,下左上右.......
int main()
{
int x,y;
int count;
int length=(int)sqrt(MAX);
int num[length][length];
memset(num,0,sizeof(num));
num[x=0][y=length-1]=count=1;
while(count<MAX){
while(x+1<length&&!num[x+1][y]) //不能出界,且当前位置未填数
num[++x][y]=++count;
while(y-1>=0&&!num[x][y-1])
num[x][--y]=++count;
while(x-1>=0&&!num[x-1][y])
num[--x][y]=++count;
while(y+1<length&&!num[x][y+1])
num[x][++y]=++count;
}
for(int i=0;i<length;i++){
for(int j=0;j<length;j++)
printf("%-4d",num[i][j]);
putchar('\n');
}
return 0;
}
蛇形填数
最新推荐文章于 2022-04-04 20:44:07 发布