페이지

1913번: 달팽이

https://www.acmicpc.net/problem/1913


$O(n^2)$


#include<cstdio>
const int fx[] = { 1,0,-1,0 }, fy[] = { 0,1,0,-1 };
int n, m, d, x, y, rx, ry, a[999][999];
int main() {
    scanf("%d %d", &n, &m);
    for (int i = n*n; i; i--) {
        a[x][y] = i;
        if (i == m) rx = x, ry = y;
        int tx = x + fx[d], ty = y + fy[d];
        if (tx < 0 || ty < 0 || tx >= n || ty >= n || a[tx][ty]) d = (d + 1) % 4;
        x += fx[d]; y += fy[d];
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) printf("%d ", a[i][j]);
        puts("");
    }
    printf("%d %d", rx + 1, ry + 1);
    return 0;
}

댓글 없음 :

댓글 쓰기