-
[SWEA 1954] 달팽이 숫자C 프로그래밍/SWEA 2022. 11. 17. 01:23728x90
https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV5PobmqAPoDFAUq
#include <cstdio> #include <cstring> int T; int N; int S[10 + 2][10 + 2]; void draw_snail() { // init int num = 1; int x = 0; int y = 0; int p = 0; S[x][y] = num; // dir int dir_x[4] = { 0, 1, 0, -1 }; int dir_y[4] = { 1, 0, -1, 0 }; while (1) { if (num == N * N) break; int nx = x + dir_x[p]; int ny = y + dir_y[p]; if (nx < 0 || nx > N - 1 || ny < 0 || ny > N - 1 || S[nx][ny] != 0) { p = (p + 1) % 4; continue; } num++; S[nx][ny] = num; x = nx; y = ny; } } void output(int t) { printf("#%d\n", t); for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { printf("%d ", S[i][j]); } printf("\n"); } } int main() { scanf("%d", &T); for (int t = 1; t <= T; t++) { memset(S, 0, sizeof(S)); scanf("%d", &N); draw_snail(); output(t); } return 0; }
728x90'C 프로그래밍 > SWEA' 카테고리의 다른 글
[SWEA 1249] 보급로 (0) 2022.11.17 [SWEA 5648] 원자 소멸 시뮬레이션 (0) 2022.11.17 [SWEA 2805] 농작물 수확하기 (0) 2022.11.17 [SWEA 1974] 스도쿠 검증 (0) 2022.11.17 [SWEA 1206] View (0) 2022.11.17