#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#define UP 0
#define RIGHT 1
#define DOWN 2
#define LEFT 3
int main() {
int moves[4] = {0, 1, 0, -1};
int *arr;
int size = 5;
int direction = RIGHT;
int index = 0;
scanf("%d", &size);
arr = (int*)calloc(size * size, sizeof(int));
moves[UP] = -size;
moves[DOWN] = size;
for (int i = 1; i <= size * size; i++) {
if (index == size - 1 || index == size * size - 1 || index == size * (size - 1) || arr[index + moves[direction]] != 0)
direction = (direction + 1) % 4;
arr[index] = i;
index += moves[direction];
}
for (int i = 0; i < size * size; i++) {
printf("%3d ", arr[i]);
if (!((i + 1) % size))
printf("\n");
}
}
딱히 최대한으로 일반화하는게 좋지만은 않은거같아...
배열 안쓰는건...되나?