diff --git "a/minyoung/week8/\353\213\254\355\214\275\354\235\264\353\254\270\354\240\234.c" "b/minyoung/week8/\353\213\254\355\214\275\354\235\264\353\254\270\354\240\234.c" new file mode 100644 index 0000000..43ceccc --- /dev/null +++ "b/minyoung/week8/\353\213\254\355\214\275\354\235\264\353\254\270\354\240\234.c" @@ -0,0 +1,50 @@ +#include +#include +int dir; + + +int* solution(int n) { + int size = n * (n + 1) / 2; + int* answer = (int*)malloc(sizeof(int) * size); + + // 배열 저장할 곳(2차원 매트릭스) 생성 + int** arr = (int**)malloc(sizeof(int*) * n); + for (int i = 0; i < n; i++) { + arr[i] = (int*)calloc(i + 1, sizeof(int)); + } + + int row = -1, col = 0; + int num = 1; + + // 로직 진행 + for (int step = n; step > 0; step--) { + // 방향 세팅 + dir = (n - step) % 3; + + for (int i = 0; i < step; i++) { + if (dir == 0) { // 아래로 + row++; + } else if (dir == 1) { // 오른쪽 + col++; + } else { // 좌상향 + row--; + col--; + } + arr[row][col] = num++; + } + } + + int idx = 0; + for (int i = 0; i < n; i++) { + for (int j = 0; j <= i; j++) { + answer[idx++] = arr[i][j]; + } + } + + for (int i = 0; i < n; i++) { + free(arr[i]); + } + free(arr); + + return answer; +} diff --git "a/minyoung/week8/\355\225\230\353\205\270\354\235\264.c" "b/minyoung/week8/\355\225\230\353\205\270\354\235\264.c" new file mode 100644 index 0000000..0124494 --- /dev/null +++ "b/minyoung/week8/\355\225\230\353\205\270\354\235\264.c" @@ -0,0 +1,47 @@ +#include +#include + +int **answer; +int idx = 0; + +// 옮길 원반 갯수 +// from 원래 원반들이 있는 곳 +// via 일단 옮겨 두는 곳 +// to 옮길 목적지 +void hanoi(int n, int from, int via, int to) { + if (n == 1) { + answer[idx][0] = from; + answer[idx][1] = to; + idx++; + return; + } + + hanoi(n - 1, from, to, via); // 위에 있는 n-1개 이동 + hanoi(1, from, via, to); // 가장 큰 원판 이동 + hanoi(n - 1, via, from, to); // 다시 n-1개 이동 +} + +int** solution(int n, int* returnSize, int** returnColumnSizes) { + // 2^n - 1 계산 -> 원반 이동할 총 횟수 알기 위해 + int total = 1; + for (int i = 0; i < n; i++) { + total *= 2; + } + total -= 1; + + // 이동경로 저장할 곳 생성 + answer = (int**)malloc(sizeof(int*) * total); + *returnColumnSizes = (int*)malloc(sizeof(int) * total); + + for (int i = 0; i < total; i++) { + answer[i] = (int*)malloc(sizeof(int) * 2); + (*returnColumnSizes)[i] = 2; + } + + idx = 0; + hanoi(n, 1, 2, 3); + + *returnSize = total; + return answer; +} +