蛇形填数 / 回形填数

#include 
#define N 5

int a[N][N] = { 0 };
int main(void){
  int x, y, tot = 0;
  tot = a[x = 0][y = N-1] = 1;

  while( tot < N*N ){
    while( x+1 < N && !a[x+1][y] )
      a[++x][y] = ++tot;
    while( y-1 >= 0 && !a[x][y-1] )
      a[x][--y] = ++tot;
    while( x-1 < N && !a[x-1][y] )
      a[--x][y] = ++tot;
    while( y+1 < N && !a[x][y+1] )
      a[x][++y] = ++tot;
  }

  for (x = 0; x < N; x++){
    for (y = 0; y < N; y++)
      printf("%d", a[x][y]);
    printf("\n");
  }
  return 0;
}

你可能感兴趣的:(蛇形填数 / 回形填数)