#include <cstdio>
#include <cstring>
int arr[10][10];
int main(){
int n = 4;
memset(arr, 0, sizeof(arr));
int x = 0;
int y = n - 1;
int count = arr[x][y] = 1;
while (count < n * n){
while (x + 1 < n && arr[x + 1][y] == 0){
arr[++x][y] = ++count;
}
while (y - 1 >= 0 && arr[x][y - 1] == 0){
arr[x][--y] = ++count;
}
while (x - 1 >= 0 && arr[x - 1][y] == 0){
arr[--x][y] = ++count;
}
while (y + 1 < n && arr[x][y + 1] == 0){
arr[x][++y] = ++count;
}
}
for (int i = 0; i < n; ++i){
for (int j = 0; j < n; ++j){
printf("%d\t", arr[i][j]);
}
printf("\n");
}
}
算法竞赛入门经典 例3-2蛇形填数
最新推荐文章于 2022-08-26 09:19:56 发布