๋ฐฑ์ค€/C++

[๋ฐฑ์ค€/C++] 17143๋ฒˆ ๋‚š์‹œ์™•

yulee_to 2022. 7. 26. 17:23

๋ฐฑ์ค€

 


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

์ƒ์–ด๋Š” ์ฃผ์–ด์ง„ ์†๋„์™€ ๋ฐฉํ–ฅ์œผ๋กœ 1์ดˆ๋งˆ๋‹ค ์ด๋™ํ•˜๊ณ , ๊ฐ™์€ ์œ„์น˜์— ๋‘๋งˆ๋ฆฌ ์ด์ƒ์ด ์ด๋™๋œ ๊ฒฝ์šฐ ์‚ฌ์ด์ฆˆ๊ฐ€ ํฐ ์ƒ์–ด๋งŒ ๋‚จ๊ฒŒ ๋œ๋‹ค.

๋‚š์‹œ์™•๋„ 1์ดˆ๋งˆ๋‹ค ์˜ค๋ฅธ์ชฝ์œผ๋กœ ์ด๋™ํ•ด ๋‚š์‹œ์™•์ด ์œ„์น˜ํ•œ ์—ด์— ์žˆ๋Š” ์ƒ์–ด ์ค‘ ๊ฐ€์žฅ ์œ„์— ์žˆ๋Š” ์ƒ์–ด๋งŒ ์žก์•„ ์˜ฌ๋ฆฌ๊ณ  ์žก์€ ์ƒ์–ด์˜ ํฌ๊ธฐ๋ฅผ ๋ชจ๋‘ ํ•ฉํ•œ ๊ฐ’์„ ์ถœ๋ ฅํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค.


๐Ÿ’ก์ฒซ๋ฒˆ์งธ ์•„์ด๋””์–ด

๋‹จ์ˆœํ•˜๊ฒŒ ๊ตฌํ˜„ํ•˜๋Š” ๋ฌธ์ œ์ธ๋ฐ ์‹œ๊ฐ„์ด ์ ๊ฒŒ ๊ฑธ๋ฆฌ๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ƒ์–ด๋ฅผ ์ด๋™์‹œํ‚ฌ ๋•Œ ๊ฒฉ์žํŒ์˜ ๊ฒฝ๊ณ„๋ฅผ ๋งŒ๋‚ฌ์„ ๋•Œ๋งˆ๋‹ค ๋ฐฉํ–ฅ์„ ๋ฐ”๊พธ๋Š”๊ฒŒ ์•„๋‹Œ ํ•œ๋ฒˆ์— ์˜ฎ๊ธฐ๋Š” ์‹์„ ์„ธ์›Œ์•ผ ํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ๋‹ค. ์—ฌ๋Ÿฌ ๊ฒฝ์šฐ๋ฅผ ์‹œ๋ฎฌ๋ ˆ์ด์…˜ ํ•ด๋ณด๊ณ  ์‹์„ ์„ธ์› ๋‹ค. 

 

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

๊ฒฉ์žํŒ์˜ ํฌ๊ธฐ๋ฅผ ๋„˜์–ด๊ฐ„๋‹ค๋ฉด ์ฒ˜์Œ ๋„˜์–ด๊ฐ€๊ณ  ๊ทธ ์ดํ›„๋ถ€ํ„ฐ๋Š” (๊ฒฉ์žํŒ ํฌ๊ธฐ-1)๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€๊ฐ€ ๊ทœ์น™์ ์œผ๋กœ 0, 1, ... (๊ฒฉ์žํŒ ํฌ๊ธฐ -1) , ...,1, 0์ด ๋˜๊ฒŒ ํ•ด์ฃผ์—ˆ๋‹ค.

 

๊ฒฉ์žํŒ์—์„œ +๋ฐฉํ–ฅ์œผ๋กœ ์ด๋™ํ•˜๋Š” ์ƒ์–ด(์•„๋ž˜์ชฝ, ์œ„์ชฝ)๋Š”

ํ˜„์žฌ ์œ„์น˜์—์„œ ์†๋„๋ฅผ ๋”ํ–ˆ์„ ๋•Œ ๊ฒฉ์žํŒ์˜ ํฌ๊ธฐ(์•„๋ž˜์ชฝ-C, ์œ„์ชฝ-R)๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์œผ๋ฉด ๋‹จ์ˆœํžˆ ๋”ํ•˜๊ธฐ๋งŒ ํ•ด์ฃผ๊ณ , ๊ฐ™์„ ๋• ๋ฐฉํ–ฅ๋„ ๋ฐ”๊ฟ”์ค€๋‹ค.

๊ฒฉ์žํŒ์„ ๋„˜์–ด๊ฐ€๋ฉด ์œ„ ๊ทœ์น™์— ๋งž๊ฒŒ ์ƒˆ๋กœ์šด ์†๋„ ๊ฐ’์„ ๊ธฐ์กด ์†๋„ + (๊ฒฉ์ž ํฌ๊ธฐ- (๊ฒฉ์ž ํฌ๊ธฐ - ํ˜„์žฌ ์œ„์น˜ + 1))๋กœ ๋ฐ”๊ฟ”์ค€๋‹ค.

๋ฐ”๊พผ ์†๋„๊ฐ’์„ (๊ฒฉ์žํฌ๊ธฐ -1)๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€๊ฐ€ ํ™€์ˆ˜๋ฉด ๋ฐฉํ–ฅ์ด ๋ฐ˜๋Œ€, ์œ„์น˜๋Š” ๊ฒฉ์ž ํฌ๊ธฐ - ( ์ƒˆ ์†๋„ % (๊ฒฉ์ž ํฌ๊ธฐ-1) )์ด ๋œ๋‹ค.

๋ฐ”๊พผ ์†๋„๊ฐ’์„ (๊ฒฉ์žํฌ๊ธฐ -1)๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€๊ฐ€ ์ง์ˆ˜๋ฉด ๋ฐฉํ–ฅ์€ ๊ทธ๋Œ€๋กœ, ์œ„์น˜๋Š” ์ƒˆ ์†๋„ % (๊ฒฉ์žํฌ๊ธฐ -1) +1 ์ด ๋œ๋‹ค.

 

๊ฒฉ์žํŒ์—์„œ -๋ฐฉํ–ฅ์œผ๋กœ ์ด๋™ํ•˜๋Š” ์ƒ์–ด(์™ผ์ชฝ, ์•„๋ž˜์ชฝ)๋Š”

ํ˜„์žฌ ์œ„์น˜์—์„œ ์†๋„๋ฅผ ๋บ์„ ๋•Œ ๊ฒฉ์žํŒ์˜ ํฌ๊ธฐ(์™ผ์ชฝ-C, ์•„๋ž˜์ชฝ-R)๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์œผ๋ฉด ๋‹จ์ˆœํžˆ ๋นผ์ฃผ๊ธฐ๋งŒ ํ•˜๊ณ , ๊ฐ™์„ ๋• ๋ฐฉํ–ฅ๋„ ๋ฐ”๊ฟ”์ค€๋‹ค.

๊ฒฉ์žํŒ์„ ๋„˜์–ด๊ฐ€๋ฉด ์œ„ ๊ทœ์น™์— ๋งž๊ฒŒ ์ƒˆ๋กœ์šด ์†๋„ ๊ฐ’์„ ๊ธฐ์กด ์†๋„ + ((๊ฒฉ์žํฌ๊ธฐ-1) - ํ˜„์žฌ์œ„์น˜)๋กœ ๋ฐ”๊ฟ”์ค€๋‹ค.

๋ฐ”๊พผ ์†๋„๊ฐ’์„ (๊ฒฉ์žํฌ๊ธฐ-1)๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€๊ฐ€ ํ™€์ˆ˜๋ฉด ๋ฐฉํ–ฅ์ด ๋ฐ˜๋Œ€, ์œ„์น˜๋Š” ์ƒˆ ์†๋„ % (๊ฒฉ์žํฌ๊ธฐ -1) +2์ด ๋œ๋‹ค.

๋ฐ”๊พผ ์†๋„๊ฐ’์„ (๊ฒฉ์žํฌ๊ธฐ-1)๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€๊ฐ€ ์ง์ˆ˜๋ฉด ๋ฐฉํ–ฅ์€ ๊ทธ๋Œ€๋กœ, ์œ„์น˜๋Š” (๊ฒฉ์ž ํฌ๊ธฐ-1) - (์ƒˆ ์†๋„ % (๊ฒฉ์žํฌ๊ธฐ-1))์ด ๋œ๋‹ค.

 

์ด๋™ํ•œ ์ƒ์–ด์˜ ์œ„์น˜๋Š” tmp๋ผ๋Š” ์ž„์‹œ ๋ฐฐ์—ด์— ์ €์žฅํ•ด์ฃผ๊ณ  ์ด๋™ํ•  tmp์˜ ์œ„์น˜์— ์ด๋ฏธ ์ƒ์–ด๊ฐ€ ์žˆ๋‹ค๋ฉด ํฌ๊ธฐ๋ฅผ ๋น„๊ตํ•ด ํฐ ๊ฒƒ๋งŒ ๋‚จ๊ฒŒ ํ•ด์ค€๋‹ค.

๋‹ค ์ด๋™์‹œํ‚จ ๋’ค์—” tmp๋ฅผ ๊ธฐ์กด ๋ฐฐ์—ด์— ์˜ฎ๊ฒจ์ฃผ๊ณ  tmp๋Š” ์ดˆ๊ธฐํ™”ํ•ด์ค€๋‹ค.

๋งค ์ดˆ๋งˆ๋‹ค ๋‚š์‹œ๊พผ์ด ์œ„์น˜ํ•œ ์—ด์˜ ๊ฐ€์žฅ ๊ฐ€๊นŒ์ด ์žˆ๋Š” ์ƒ์–ด๋ฅผ ์žก๊ณ  ๊ทธ ํฌ๊ธฐ๋ฅผ ๋‹ค ๋”ํ•ด์ฃผ๋ฉด ๋!

 

์„ค๋ช…์ด ๋‹ค์†Œ ๋ณต์žกํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ์–ด๋Š ํ•œ ์œ„์น˜์— ์žˆ๋Š” ์ƒ์–ด๋ฅผ ์—ฌ๋Ÿฌ ์†๋„๋กœ ์ด๋™์‹œํ‚ฌ ๋•Œ ์œ„ ๋ฐฉ์‹์„ ์ ์šฉํ•ด๋ณด๋ฉด ์–ด๋–ค ๋Š๋‚Œ์ธ์ง€ ์•Œ๊ฒŒ ๋  ๊ฑฐ๋‹ค.

 

 

โŒํŠธ๋Ÿฌ๋ธ” ์ŠˆํŒ…

๊ฐ ๋ฐฉํ–ฅ๋งˆ๋‹ค ๊ฒฉ์žํŒ์˜ ํฌ๊ธฐ์™€ ์ƒ์–ด์˜ ์œ„์น˜๋ฅผ ๊ณ„์‚ฐํ•  ๋•Œ ํ•„์š”ํ•œ ์ขŒํ‘œ๊ฐ’ ํ•˜๋‚˜๊ฐ€ ๋‹ค๋ฅธ ์ ์„ ์ƒ๊ฐํ•˜์ง€ ๋ชปํ–ˆ๋‹ค. +๋ฐฉํ–ฅ๊ณผ -๋ฐฉํ–ฅ๋ผ๋ฆฌ ๋™์ž‘์ด ๊ฐ™๋‹ค๊ณ  ํ•ด์„œ ๋ณต๋ถ™๋งŒ ํ–ˆ๋˜๊ฒŒ ์‹ค์ˆ˜์˜€๊ณ , ์ž…๋ ฅ๋ฐ›์€ ๋ณ€์ˆ˜ ์ค‘์— ๊ฒฉ์žํฌ๊ธฐ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” R,C์™€ ์ƒ์–ด์˜ ์œ„์น˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” r,c๋ž‘ ํ—ท๊ฐˆ๋ ธ์—ˆ๋‹ค. ์ฝ”๋“œ๊ฐ€ ๊ธธ์–ด์ง€๊ณ  ์ž…๋ ฅ ๋ถ€๋ถ„์„ ๋ฏธ๋ฆฌ ์งœ๋‘๊ณ  ์ƒ์–ด ์ด๋™ ์‹์„ ์„ธ์šฐ๋‹ค๋ณด๋‹ˆ ๊นŒ๋จน์€ ๊ฑฐ ๊ฐ™๋‹ค..ใ…Ž


#include <iostream>
#include <cstring>

using namespace std;

int R, C, M;
int r, c, s, d, z;
pair<int, pair<int, int>> map[102][102]; //์†๋ ฅ, ์ด๋™๋ฐฉํ–ฅ, ํฌ๊ธฐ
pair<int, pair<int, int>> tmp[102][102];

int main() {
    cin.tie(nullptr);
    cout.tie(nullptr);
    ios::sync_with_stdio(false);
    cin >> R >> C >> M;
    for (int i = 0; i < M; i++) {
        cin >> r >> c >> s >> d >> z;
        map[r][c] = {s, {d, z}};
    }
    int cnt = 0;
    for (int k = 1; k <= C; k++) {
        for (int i = 1; i <= R; i++) {
            if (map[i][k].second.first == 0) continue;
            else {
                map[i][k].second.first = 0;
                cnt += map[i][k].second.second;
                break;
            }
        }
        for (int i = 1; i <= R; i++) {
            for (int j = 1; j <= C; j++) {
                int y = i;
                int x = j;
                int speed = map[i][j].first;
                int direct = map[i][j].second.first;
                int size = map[i][j].second.second;
                if (direct == 0) continue;
                switch (direct) {
                    case 1:
                        if ((y - speed) > 1) {
                            y = y - speed;
                        } else if ((y - speed) == 1) {
                            y = y - speed;
                            direct = 2;
                        } else {
                            int ny = speed + ((R - 1) - y);
                            if (ny / (R - 1) % 2 == 1) {
                                direct = 2;
                                y = ny % (R - 1) + 2;
                            } else {
                                y = (R - 1) - (ny % (R - 1));
                            }
                        }

                        if (tmp[y][x].second.second != 0) {
                            if (tmp[y][x].second.second < size) {
                                tmp[y][x] = {speed, {direct, size}};
                            }
                        } else {
                            tmp[y][x] = {speed, {direct, size}};
                        }
                        break;
                    case 2:
                        if ((y + speed) < R) {
                            y = y + speed;
                        } else if ((y + speed) == R) {
                            y = y + speed;
                            direct = 1;
                        } else {
                            int ny = speed + (R - (R - y + 1));
                            if (ny / (R - 1) % 2 == 1) {
                                direct = 1;
                                y = R - (ny % (R - 1));
                            } else {
                                y = (ny % (R - 1)) + 1;
                            }
                        }

                        if (tmp[y][x].second.second != 0) {
                            if (tmp[y][x].second.second < size) {
                                tmp[y][x] = {speed, {direct, size}};
                            }
                        } else {
                            tmp[y][x] = {speed, {direct, size}};
                        }
                        break;
                    case 3:
                        if ((x + speed) < C) {
                            x = x + speed;
                        } else if ((x + speed) == C) {
                            x = x + speed;
                            direct = 4;
                        } else {
                            int nx = speed + (C - (C - x + 1));
                            if (nx / (C - 1) % 2 == 1) {
                                direct = 4;
                                x = C - (nx % (C - 1));
                            } else {
                                x = (nx % (C - 1)) + 1;
                            }
                        }

                        if (tmp[y][x].second.second != 0) {
                            if (tmp[y][x].second.second < size) {
                                tmp[y][x] = {speed, {direct, size}};
                            }
                        } else {
                            tmp[y][x] = {speed, {direct, size}};
                        }
                        break;
                    case 4:
                        if ((x - speed) > 1) {
                            x = x - speed;
                        } else if ((x - speed) == 1) {
                            x = x - speed;
                            direct = 3;
                        } else {
                            int nx = speed + ((C - 1) - x);
                            if (nx / (C - 1) % 2 == 1) {
                                direct = 3;
                                x = nx % (C - 1) + 2;
                            } else {
                                x = (C - 1) - (nx % (C - 1));
                            }
                        }

                        if (tmp[y][x].second.second != 0) {
                            if (tmp[y][x].second.second < size) {
                                tmp[y][x] = {speed, {direct, size}};
                            }
                        } else {
                            tmp[y][x] = {speed, {direct, size}};
                        }
                        break;
                }
            }

        }
        for (int i = 1; i <= R; i++) {
            for (int j = 1; j <= C; j++) {
                map[i][j] = tmp[i][j];
            }
        }
        memset(tmp, 0, sizeof(tmp));
    }
    cout << cnt;
}
728x90