ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [20057] 마법사 상어와 토네이도
    BOJ 2022. 1. 23. 09:22

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

     

    20057번: 마법사 상어와 토네이도

    마법사 상어가 토네이도를 배웠고, 오늘은 토네이도를 크기가 N×N인 격자로 나누어진 모래밭에서 연습하려고 한다. 위치 (r, c)는 격자의 r행 c열을 의미하고, A[r][c]는 (r, c)에 있는 모래의 양을

    www.acmicpc.net

    <문제>

    토네이도의 이동 방향은 '좌하우상'의 반복이고, 길이는 {1, 1, 2, 2, 3, 3...}같은 형태로, 2번마다 1씩 증가하는 형태이다.

    문제에 제시된 '일정한 비율'을 db로 넣어준다.

    해당 db는 회전의 용이성을 이유로 일부러 5*5의 형태로 잡아두었고

    토네이도의 방향이 회전할때마다 db도 같이 회전시켜주었다.

    소수점 아래를 버리는 부분은 int간의 '/'연산으로 자연스레 해결되는 부분이며

    알파부분에 들어가는 모래 양을 잘 계산해줘야 하는데, 식을 한 번에 묶지 말고 전개하여 계산해야 한다.
    (55%로 계산하면 틀리는 거로 확인했는데, 반례 찾기도 쉽지 않아 보인다)

    구현 난이도가 꽤 높은 문제였다.

     

    <소스코드>

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    #include <bits/stdc++.h>
    using namespace std;
    const int power[5][5= {
        {00200},  {010710}, {555000},
        {010710}, {00200},
    };
    int n, ans, dy[4= {010-1}, dx[4= {-1010};  //좌하우상
    int curPower[5][5], a[501][501];
    void updateAnswer(int score) { ans += score; }
    int c(int base) {
        int ret = base;
        ret = ret - ((1 * base) / 100);
        ret = ret - ((1 * base) / 100);
     
        ret = ret - ((2 * base) / 100);
        ret = ret - ((2 * base) / 100);
     
        ret = ret - ((7 * base) / 100);
        ret = ret - ((7 * base) / 100);
     
        ret = ret - ((10 * base) / 100);
        ret = ret - ((10 * base) / 100);
     
        ret = ret - ((5 * base) / 100);
        return ret;
    }
    void f(int cy, int cx) {  //(cy,cx)==(2,2);y
        int base = a[cy][cx];
        if (base == 0return;
        a[cy][cx] = 0;
        for (int y = -2; y <= 2; y++) {
            for (int x = -2; x <= 2; x++) {
                if (curPower[y + 2][x + 2== 0continue;
                int cur = 0;
                if (curPower[y + 2][x + 2== 55)
                    cur = c(base);
                else
                    cur = (curPower[y + 2][x + 2* base) / 100;
                int curY = cy + y;
                int curX = cx + x;
                if (curY < 0 || curX < 0 || curY >= n || curX >= n) {
                    updateAnswer(cur);
                    continue;
                }
                a[curY][curX] += cur;
            }
        }
    }
    void updatePower(void) {
        int i, j, temp[5][5];
        for (i = 0; i < 5; i++)
            for (j = 0; j < 5; j++) temp[i][j] = curPower[i][j];
     
        for (i = 0; i < 5; i++)
            for (j = 0; j < 5; j++) curPower[5 - j - 1][i] = temp[i][j];
        return;
    }
    int main(void) {
        ios_base::sync_with_stdio(0);
        cin.tie(0);
        cin >> n;
        int i, j;
        for (i = 0; i < n; i++)
            for (j = 0; j < n; j++cin >> a[i][j];
        int l = 1, y = n / 2, x = n / 2, dir = 0;
        for (i = 0; i < 5; i++)
            for (j = 0; j < 5; j++) curPower[i][j] = power[i][j];
        while (1) {
            if (y == 0 && x == 0break;
            for (i = 0; i < 2; i++) {
                for (j = 0; j < l; j++) {
                    y += dy[dir];
                    x += dx[dir];
                    f(y, x);
                    if (y == 0 && x == 0) {
                        cout << ans;
                        return 0;
                    }
                }
                updatePower();
                dir++;
                if (dir == 4) dir = 0;
            }
            l++;
        }
        cout << ans;
        return 0;
    }
    cs

     

    'BOJ' 카테고리의 다른 글

    [1113] 수영장 만들기  (0) 2022.01.23
    [3687]성냥개비  (0) 2022.01.23
    [16208] 귀찮음  (0) 2022.01.17
    [11444] 피보나치 수 6  (0) 2022.01.16
    [1774] 우주신과의 교감  (0) 2022.01.15

    댓글