๋ฐฑ์ค€/C++

[๋ฐฑ์ค€/C++] 21610๋ฒˆ ๋งˆ๋ฒ•์‚ฌ ์ƒ์–ด์™€ ๋น„๋ฐ”๋ผ๊ธฐ

yulee_to 2022. 10. 6. 23:36

๋ฐฑ์ค€

 


๐Ÿค”๋ฌธ์ œ ์ดํ•ด

๊ทธ๋ƒฅ ๋‹จ์ˆœ ๊ตฌํ˜„ ๋ฌธ์ œ๋กœ ์ž˜ ์ฝ๊ณ  ์กฐ๊ฑด์— ๋งž์ถฐ ๋น„๊ตฌ๋ฆ„์„ ์˜ฎ๊ฒจ์ฃผ๋ฉด ๋˜๋Š” ๋ฌธ์ œ์˜€๋‹ค. 


๐Ÿ”ฅํ’€์ด๐Ÿ”ฅ

(i, j)์— ์œ„์น˜ํ•œ ๋ฐ”๊ตฌ๋‹ˆ์— ๋‹ด๊ธด ๋ฌผ์˜ ์–‘์„ ๋‚˜ํƒ€๋‚ด๋Š” ๋ฐฐ์—ด map[i][j]

๋ช…๋ น ๊ณผ์ • 3๋ฒˆ์—์„œ ์‚ฌ๋ผ์ง„ ๊ตฌ๋ฆ„์„ ํ‘œ์‹œํ•˜๋Š” ๋ฐฐ์—ด visited[i][j]

๋ช…๋ น์—์„œ ์ฃผ๋Š” ๋ฐฉํ–ฅ์„ ์˜๋ฏธํ•˜๋Š” di[k]์™€ dj[k]

๊ฑฐ๋ฆฌ๊ฐ€ 1์ธ ๋Œ€๊ฐ์„ ์„ ์˜๋ฏธํ•˜๋Š” diag_i[k]์™€ diag_j[k]

๊ตฌ๋ฆ„์˜ ์œ„์น˜๋ฅผ ์ €์žฅํ•  ํ cloud

 

m๋ฒˆ์˜ ๋ช…๋ น๋งˆ๋‹ค ์•„๋ž˜ ๊ณผ์ •์„ ์ˆ˜ํ–‰ํ•ด์ค€๋‹ค.

1. cloud์˜ front ๋น„๊ตฌ๋ฆ„์˜ ์œ„์น˜์— ๋ฐฉํ–ฅ * ๊ฑฐ๋ฆฌ๊ฐ’์„ ๋”ํ•ด์ฃผ๊ณ  ๊ฒฉ์žํฌ๊ธฐ(N)๋งŒํผ ๋‚˜๋จธ์ง€ ์—ฐ์‚ฐ

2. ๊ณ„์‚ฐ๋œ ์œ„์น˜๊ฐ’์ด ์Œ์ˆ˜์ด๊ฑฐ๋‚˜ 0์ด๋ฉด +N

3. popํ•ด์ฃผ๊ณ  ๊ณ„์‚ฐ๋œ ์œ„์น˜๊ฐ’์˜ ๋น„์˜ ์–‘์„ +1ํ•˜๊ณ  ์œ„์น˜๊ฐ’์„ cloud์— push

4. ์˜ฎ๊ฒจ์ง„ ๋น„๊ตฌ๋ฆ„์— ๋Œ€ํ•ด ๋Œ€๊ฐ์„  ๊ฑฐ๋ฆฌ1์— ๋ฌผ์ด ์žˆ๋Š” ๋ฐ”๊ตฌ๋‹ˆ์˜ ๊ฐœ์ˆ˜๋งŒํผ ๋น„๊ตฌ๋ฆ„์ด ์œ„์น˜ํ•œ ๋ฐ”๊ตฌ๋‹ˆ์— ๋ฌผ ์ถ”๊ฐ€

5. ๋ฌธ์ œ์˜ ๋ช…๋ น ๊ณผ์ • 3๋ฒˆ์—์„œ ์‚ฌ๋ผ์ง„ ๊ตฌ๋ฆ„์ž„์„ visited์— ํ‘œ์‹œ

6. ๋ชจ๋“  ๋ฐ”๊ตฌ๋‹ˆ๋ฅผ ๋Œ๋ฉด์„œ ๋ช…๋ น ๊ณผ์ • 3๋ฒˆ์—์„œ ์‚ฌ๋ผ์ง„ ๊ตฌ๋ฆ„์ด ์•„๋‹ˆ๋ฉด์„œ ๋ฐ”๊ตฌ๋‹ˆ์— ๋ฌผ์ด 2์ด์ƒ ์žˆ๋Š” ๊ณณ์˜ ์œ„์น˜๋ฅผ cloud์— ์ถ”๊ฐ€ํ•˜๊ณ  ๋ฐ”๊ตฌ๋‹ˆ์—์„œ ๋ฌผ 2 ๋บŒ

7. visited ์ดˆ๊ธฐํ™”

 

๋งˆ์ง€๋ง‰์—” map์˜ ๋ชจ๋“  ๊ฐ’์„ ๋‹ค ๋”ํ•ด ์ถœ๋ ฅํ•ด์ค€๋‹ค.


#include <iostream>
#include <queue>
#include <cstring>

using namespace std;

int n, m, r, c, d, s;
int map[51][51];
bool visited[51][51];
int di[9] = {0, 0, -1, -1, -1, 0, 1, 1, 1};
int dj[9] = {0, -1, -1, 0, 1, 1, 1, 0, -1};
int diag_i[4] = {-1, -1, 1, 1};
int diag_j[4] = {-1, 1, -1, 1};
queue<pair<int, int>> cloud;

int main() {
    cin.tie(nullptr);
    cout.tie(nullptr);
    ios::sync_with_stdio(false);

    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            cin >> map[i][j];
        }
    }

    cloud.push({n, 1});
    cloud.push({n, 2});
    cloud.push({n - 1, 1});
    cloud.push({n - 1, 2});

    for (int i = 0; i < m; i++) {
        cin >> d >> s;
        int size = cloud.size();
        for (int j = 0; j < size; j++) {
            r = (cloud.front().first + di[d] * s) % n;
            c = (cloud.front().second + dj[d] * s) % n;
            if (r <= 0) {
                r += n;
            }
            if (c <= 0) {
                c += n;
            }
            cloud.pop();
            cloud.push({r, c});
            map[r][c]++;
        }
        while (!cloud.empty()) {
            int cnt = 0;
            r = cloud.front().first;
            c = cloud.front().second;
            cloud.pop();
            for (int k = 0; k < 4; k++) {
                int mr = r + diag_i[k];
                int mc = c + diag_j[k];
                if (mr <= 0 || mc <= 0 || mr > n || mc > n) continue;
                if (map[mr][mc] > 0)
                    cnt++;
            }
            visited[r][c] = true;
            map[r][c] += cnt;
        }
        for (int j = 1; j <= n; j++) {
            for (int k = 1; k <= n; k++) {
                if (map[j][k] >= 2 && !visited[j][k]) {
                    cloud.push({j, k});
                    map[j][k] -= 2;
                }
            }
        }
        memset(visited, false, sizeof(visited));
    }
    int total = 0;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            total += map[i][j];
        }
    }
    cout << total;
}

 

728x90