Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

18-yuyu0830 #68

Merged
merged 2 commits into from
Jul 16, 2024
Merged

18-yuyu0830 #68

merged 2 commits into from
Jul 16, 2024

Conversation

yuyu0830
Copy link
Collaborator

@yuyu0830 yuyu0830 commented Jul 5, 2024

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

2048 (Easy) (12100)

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

3์‹œ๊ฐ„
๊ตฌํ˜„ ๋ฌธ์ œ๋Š” ๊ท€์ฐฎ๋„ค์š”

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

โ“ ๋ฌธ์ œ

2048 ๊ฒŒ์ž„์„ ๋งŒ๋“ค์ž!
1 < n < 20 ์˜ ์ˆซ์ž n ๊ณผ n * n ์‚ฌ์ด์ฆˆ์˜ ๊ฒŒ์ž„ ํŒ m ๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์›€์ง์ž„์— ๊ด€ํ•œ ๋ฃฐ์€ ๋ฌธ์ œ๋ฅผ ์ฐธ๊ณ ํ•˜์ž
๊ธฐ์กด 2048์˜ ์›€์ง์ž„๋งˆ๋‹ค ์ƒ์„ฑ๋˜๋Š” ์ถ”๊ฐ€ ๋ธ”๋Ÿญ์ด ์—†๋‹ค๊ณ  ํ–ˆ์„ ๋•Œ, 5๋ฒˆ ์›€์ง์ž„ ์ด๋‚ด์—์„œ ์ตœ๋Œ€๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ์ˆ˜์˜ ๊ฐ’์„ ๊ตฌํ•˜์ž

โ— ํ’€์ด

๋กœ์ง์€ ๋‹จ์ˆœํ•˜๋‹ค! ์ง์ ‘ ์›€์ง์—ฌ๋ณด๊ณ  ์ตœ๋Œ€ ๊ฐ’์„ ๊ตฌํ•˜๋ฉด ๋œ๋‹ค. ์ฃผ์„์ด ๋งŽ์€ ๊ฒƒ ์—์„œ ๋ณผ ์ˆ˜ ์žˆ๋‹ค์‹œํ”ผ ์ข€ ๊ท€์ฐฎ๊ธด ํ•˜๋‹ค..

์ „์ฒด ๋กœ์ง

์žฌ๊ท€ ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ๋ฐฑํŠธ๋ž˜ํ‚น์„ ๊ตฌํ˜„ํ–ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ํ•จ์ˆ˜ ํ•˜๋‚˜์— ์›€์ง์ด๋Š” ํŒŒํŠธ์™€ ์žฌ๊ท€ ํŒŒํŠธ๋ฅผ ๋„ฃ์ž๋‹ˆ ๋„ˆ๋ฌด ๊ธธ์–ด์„œ ์›€์ง์ž„ ํ•จ์ˆ˜ move ์™€ ์žฌ๊ท€ ํ•จ์ˆ˜ f ๋ฅผ ๋ณ„๋„๋กœ ๊ตฌํ˜„ํ–ˆ๋‹ค.

์žฌ๊ท€ ํŒŒํŠธ

์žฌ๊ท€๋Š” ๋ณ„ ์–ด๋ ค์šด ๋ถ€๋ถ„์ด ์—†์ง€๋งŒ ๋ฐฑํŠธ๋ž˜ํ‚น์„ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•ด ์›€์ง์ž„ ํŒŒํŠธ์—์„œ ์›€์ง์ž„์ด ์—†๋‹ค๊ณ  ํŒ๋‹จ๋˜๋ฉด ๋” ์ด์ƒ ํƒ์ƒ‰ํ•˜์ง€ ์•Š๋Š” ๋กœ์ง์„ ๊ตฌํ˜„ํ–ˆ๋‹ค.

์›€์ง์ž„ ๊ตฌํ˜„

์žฌ๊ท€ ํŒŒํŠธ์—์„œ ์›€์ง์ž„ ํ•จ์ˆ˜๋กœ ํ˜„์žฌ m์˜ ์ƒํƒœ ์™€ ์›€์ง์ผ ๋ฐฉํ–ฅ d ๋ฅผ ์ „๋‹ฌํ•œ๋‹ค. d ๋ฐฉํ–ฅ์œผ๋กœ 2048 ๊ฒŒ์ž„์˜ ๋ฃฐ์— ๋งž์ถฐ ์ด๋™ํ•œ๋‹ค.

image
์—ฌ๊ธฐ์„œ ์›€์ง์ด๋Š” ๋ฐฉํ–ฅ์„ ๊ธฐ์ค€์œผ๋กœ ๊ฐ ์ค„๋งˆ๋‹ค ๋…๋ฆฝ์ ์œผ๋กœ ์—ฐ์‚ฐ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. ๊ตณ์ด ์˜† ์ค„๊นŒ์ง€ ์‹ ๊ฒฝ์“ฐ์ง€ ์•Š์•„๋„ ๋˜๋‹ˆ ์˜ˆ์‹œ์—์„œ๋Š” ํ•œ ์ค„์”ฉ ๋‹ค๋ฃจ๋„๋ก ํ•˜์ž

image

์—ฌ๊ธฐ์„œ ํƒ์ƒ‰ ๋ฐฉํ–ฅ์€ ์›€์ง์ด๋Š” ๋ฐฉํ–ฅ d์˜ ์—ญ๋ฐฉํ–ฅ์œผ๋กœ ํƒ์ƒ‰ํ•˜๋ฉด ๋œ๋‹ค.

image

์ฒซ ์ˆซ์ž์˜ ๊ฒฝ์šฐ ๋” ์—ฐ์‚ฐํ•  ๋ฐฉํ–ฅ์ด ์—†์œผ๋‹ˆ ๊ตณ์ด ์•ˆํ•ด๋„ ๋œ๋‹ค.

image

์ด๋ ‡๊ฒŒ n - 1๋ฒˆ๋งŒ ์—ฐ์‚ฐํ•˜๋ฉด ๋œ๋‹ค.

1๋ฒˆ ์นธ ๋ถ€ํ„ฐ ํƒ์ƒ‰ํ•˜์ž.

image

ํ˜„์žฌ ํƒ์ƒ‰์ค‘์ธ ์นธ ์„ a ๋ผ๊ณ  ํ•˜์ž. ํ˜„์žฌ ํƒ์ƒ‰์ค‘์ธ ์นธ์ด 0์ธ ๊ฒฝ์šฐ์—๋Š” ๊ทธ๋ƒฅ ๋„˜์–ด๊ฐ€๋ฉด ๋œ๋‹ค. 1๋ฒˆ ์นธ์€ 0์ด๋‹ˆ ๋„˜์–ด๊ฐ€์ž

image

2๋ฒˆ ์นธ์€ 0์ด ์•„๋‹ˆ๋‹ค. ๋งŒ์•ฝ 0์ด ์•„๋‹Œ ๊ฒฝ์šฐ์—๋Š” a๋ฅผ ์›€์ง์ผ ์ˆ˜ ์žˆ๋Š” ์นธ ๊นŒ์ง€ ๋ฐฉํ–ฅ d ๋Œ€๋กœ ์›€์ง์—ฌ์•ผํ•œ๋‹ค. ์–ด๋””๊นŒ์ง€ ์›€์ง์ผ์ง€ ์ž„์‹œ๋กœ ํƒ์ƒ‰ํ•  ์นธ์„ g ๋ผ๊ณ  ํ•˜์ž. g๋Š” ๊ธฐ์กด ํƒ์ƒ‰ ๋ฐฉํ–ฅ์˜ ๋ฐ˜๋Œ€๋กœ ํƒ์ƒ‰ํ•˜๊ฒŒ ๋œ๋‹ค.

image

์ด ์ˆœ์„œ๋Œ€๋กœ ์–ด๋””๊นŒ์ง€ ์›€์ง์ผ์ง€ ํƒ์ƒ‰์„ ํ•˜๋Š”๋ฐ, ๋งŒ์•ฝ 0์ด๋ฉด ํŒจ์Šคํ•œ๋‹ค.

image

ํ•˜์ง€๋งŒ 0์ด ์•„๋‹ˆ๋ฉด ์–ด๋–ป๊ฒŒ ์ด๋™์„ ํ• ์ง€ ๊ณ„์‚ฐํ•ด์•ผํ•œ๋‹ค. (์›€์ง์ž„ ๋ฐฉํ–ฅ์˜ ์—ญ ๋ฐฉํ–ฅ์œผ๋กœ ์—ฐ์‚ฐํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ์—ฌ๊ธฐ์„œ ๋งŒ๋‚œ ๊ฐ’์˜ ์•ž์€ ๋” ์ด์ƒ 0์ด ์—†๊ธฐ ๋•Œ๋ฌธ์— ์›€์ง์ผ ํ•„์š”๊ฐ€ ์—†๋‹ค). ๋งŒ์•ฝ ํ˜„์žฌ ํƒ์ƒ‰ ์ค‘์ธ ๊ฐ’๊ณผ ๊ฐ™์€ ๊ฐ’์ด๋ฉด ๋”ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๋ฏธ ๋”ํ–ˆ์œผ๋ฉด ๋” ์ด์ƒ ๋”ํ•  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— ๋ฐฉ๋ฌธ ํ˜„ํ™ฉ์„ visited ๋ฐฐ์—ด์„ ํ†ตํ•ด ์ฒดํฌํ•˜์ž.

์˜ˆ์‹œ์˜ ๊ฒฝ์šฐ๋Š” ๊ฐ’์ด ๋‹ค๋ฅด๊ธฐ ๋•Œ๋ฌธ์— ํƒ์ƒ‰ ์ค‘์ธ ๊ฐ’ ๋ฐ”๋กœ ์ „๊นŒ์ง€ ์ด๋™ํ•ด์•ผํ•œ๋‹ค.

image

image

์ด๋ ‡๊ฒŒ ์ด๋™ํ•˜๋ฉด ๋œ๋‹ค!

์œ„์˜ ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ์ „๋ถ€ ์ง€ํ‚ค๋ฉด์„œ 5๋ฒˆ ์žฌ๊ท€ํ•˜๋ฉด ๋œ๋‹ค!

#include <iostream>

using namespace std;

int map[21][21] = {0, };
int n, ans = 0;
int dir[4][2] = {
    {0, 1},
    {0, -1},
    {1, 0},
    {-1, 0}
};

bool isin(int t) { return t >= 0 && t < n; }

bool move(int m[21][21], int d) {
    // ๋ฐ˜ํ™˜ ๊ฐ’(e)์€ ์›€์ง์ž„ ์—ฌ๋ถ€
    bool e = false;

    // vertical : ์›€์ง์ž„ ๋ฐฉํ–ฅ์ด ๊ฐ€๋กœ์ธ์ง€ ์„ธ๋กœ์ธ์ง€
    // plus : ์›€์ง์ž„ ๋ฐฉํ–ฅ์ด + ๋ฐฉํ–ฅ์ธ์ง€ - ๋ฐฉํ–ฅ์ธ์ง€
    bool vertical = d < 2, plus = d % 2 == 0;

    // ํ•œ ๋ฒˆ ๋”ํ•ด์ง„ ๋ถ€๋ถ„์€ ๋‹ค์‹œ ๋”ํ•ด์ง€๋ฉด ์•ˆ๋˜๋‹ˆ๊นŒ ๋ฐฉ๋ฌธ ์ฒ˜๋ฆฌ
    bool visited[n][n];
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            visited[i][j] = 0;

    for (int i = 0; i < n; i++) {
        // ํ˜„์žฌ ํƒ์ƒ‰ ์œ„์น˜์˜ x, y ์ขŒํ‘œ๊ฐ’
        int x = vertical ? i : (plus ? 1 : n - 2);
        int y = vertical ? (plus ? 1 : n - 2) : i;

        // ํƒ์ƒ‰ ๋ฒ”์œ„ ์•ˆ์— ์žˆ๋Š” ๋™์•ˆ๋งŒ ํƒ์ƒ‰
        while (isin(vertical ? y : x)) {
            // ํ˜„์žฌ ํƒ์ƒ‰ ์œ„์น˜์— ๊ฐ’์ด ์žˆ๋‹ค๋ฉด ์ด๋™ ํ•ด์•ผํ•จ
            if (m[x][y]) {
                // ๊ฐ’ ์ €์žฅ ๋ฐ ํ˜„์žฌ ํƒ์ƒ‰ ์œ„์น˜ 0์œผ๋กœ
                int tmp = m[x][y];
                m[x][y] = 0;

                // ์–ด๋””๊นŒ์ง€ ์ด๋™ํ• ์ง€ ํƒ์ƒ‰ํ•  ์ž„์‹œ ํƒ์ƒ‰ ์œ„์น˜์˜ x, y ์ขŒํ‘œ๊ฐ’
                int tx = x - dir[d][0];
                int ty = y - dir[d][1];

                while (true) {
                    // ์ž„์‹œ ํƒ์ƒ‰ ์œ„์น˜์— ๊ฐ’์ด ์žˆ๋‹ค๋ฉด
                    if (m[tx][ty]) {
                        // ๊ฐ’์ด ์žˆ๊ณ , ํ˜„์žฌ ํƒ์ƒ‰ ์œ„์น˜์™€ ๊ฐ’์ด ๊ฐ™๊ณ , ๋ฐฉ๋ฌธํ•œ ์ ์ด ์—†๋‹ค๋ฉด
                        // ์ž„์‹œ ํƒ์ƒ‰ ์œ„์น˜์˜ ๊ฐ’์— ๋”ํ•ด์ง
                        if (tmp == m[tx][ty] && !visited[tx][ty]) {
                            e = true;
                            visited[tx][ty] = true;
                            break;
                        }
                        // ๊ฐ’์ด ๋‹ค๋ฅด๊ฑฐ๋‚˜, ๋ฐฉ๋ฌธํ•œ ์ ์ด ์žˆ์œผ๋ฉด
                        // ์ž„์‹œ ํƒ์ƒ‰ ์œ„์น˜์—์„œ ํ•œ ์นธ ์ „์œผ๋กœ ์ด๋™
                        else {
                            tx += dir[d][0];
                            ty += dir[d][1];
                            break;
                        }
                    }

                    // ๋” ์ด๋™ ๊ฐ€๋Šฅํ•œ์ง€ ์ฒดํฌ
                    if (!isin(vertical ? ty - dir[d][1]: tx - dir[d][0])) 
                        break;

                    // ์›€์ง์ž„ ์—ฌ๋ถ€ ์ฒดํฌ
                    e = true;

                    // ์ž„์‹œ ํƒ์ƒ‰ ์œ„์น˜ ์ด๋™
                    tx -= dir[d][0];
                    ty -= dir[d][1];
                }

                // ์ž„์‹œ ํƒ์ƒ‰ ์œ„์น˜๊นŒ์ง€ ์ด๋™ ์ฒ˜๋ฆฌ
                m[tx][ty] += tmp;
                // ์ตœ๋Œ€ ๊ฐ’ ์ฒ˜๋ฆฌ
                ans = max(ans, m[tx][ty]);
            }

            // ํ˜„์žฌ ํƒ์ƒ‰ ์œ„์น˜ ์ด๋™
            x += dir[d][0];
            y += dir[d][1];
        }
    }

    return e;
}

void f(int m[21][21], int depth) {
    // depth๊ฐ€ 5๊ฐ€ ๋˜๋ฉด ํƒ์ƒ‰ ์ข…๋ฃŒ
    if (depth == 5) return;

    int tm[21][21];

    for (int d = 0; d < 4; d++) {
        // ์ž„์‹œ map ์ƒ์„ฑ
        for (int i = 0; i < n; i++) 
            for (int j = 0; j < n; j++) 
                tm[i][j] = m[i][j];
            
        // ๋งŒ์•ฝ ์›€์ง์ž„์ด ์—†์œผ๋ฉด ๊ตณ์ด ๋” ํƒ์ƒ‰ํ•˜์ง€ ์•Š๋Š”๋‹ค.
        if (move(tm, d)) 
            f(tm, depth + 1);
 
    }
}

int main() {
    cin >> n;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cin >> map[i][j];
            ans = max(ans, map[i][j]);
        }
    }

    f(map, 0);
    printf("%d\n", ans);
}

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

๊ตฌํ˜„ ๋ฌธ์ œ๋Š” ์‰ฌ์šด ์ฒ™ ํ•˜๋Š” ๊ท€์ฐฎ์€ ๋ฌธ์ œ๊ฐ€ ๋Œ€๋ถ€๋ถ„์ด๋‹ค

Copy link
Collaborator

@InSange InSange left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ์•ˆ๋‚˜๋Š”๊ฒŒ ์‹ ๊ธฐํ•˜๊ตฐ์š”
๋งค ๋ฐฉํ–ฅ์˜ ์ดˆ๊ธฐ์— ๋ฐฐ์—ด์„ ์ดˆ๊ธฐํ™”์‹œํ‚ค๊ณ  4๊ฐœ์˜ ๋ฐฉํ–ฅ์„ ๋“ค์–ด์™”๋‹ค ๋‚˜์˜ค๊ณ ํ•ด์•ผํ•˜๊ณ ..
4^5์˜ ๊ฐ€์ง“์ˆ˜๊ฐ€ ๋˜๋‚˜์š”?
๊ฑฐ๊ธฐ์— ๋ฐฐ์—ด์„ ํƒ์ƒ‰ํ•˜๋Š” ๊ณผ์ •๊นŒ์ง€ ์‰ฝ์ง€์•Š์•˜๋Š”๋ฐ ์ž˜ ๋ณด๊ณ  ๊ฐ‘๋‹ˆ๋‹ค.

์š”์ƒˆ๋Š” ๋˜ ํ˜‘์—…๋„ ํ˜‘์—…์ด์ง€๋งŒ ์„ค๊ณ„๋ฅผ ์–ด๋–ป๊ฒŒ ํ•˜๋Š๋ƒ๋„ ๋”ฐ์ง€๋”๊ตฐ์š”.

@yuyu0830
Copy link
Collaborator Author

์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ์•ˆ๋‚˜๋Š”๊ฒŒ ์‹ ๊ธฐํ•˜๊ตฐ์š” ๋งค ๋ฐฉํ–ฅ์˜ ์ดˆ๊ธฐ์— ๋ฐฐ์—ด์„ ์ดˆ๊ธฐํ™”์‹œํ‚ค๊ณ  4๊ฐœ์˜ ๋ฐฉํ–ฅ์„ ๋“ค์–ด์™”๋‹ค ๋‚˜์˜ค๊ณ ํ•ด์•ผํ•˜๊ณ .. 4^5์˜ ๊ฐ€์ง“์ˆ˜๊ฐ€ ๋˜๋‚˜์š”? ๊ฑฐ๊ธฐ์— ๋ฐฐ์—ด์„ ํƒ์ƒ‰ํ•˜๋Š” ๊ณผ์ •๊นŒ์ง€ ์‰ฝ์ง€์•Š์•˜๋Š”๋ฐ ์ž˜ ๋ณด๊ณ  ๊ฐ‘๋‹ˆ๋‹ค.

์š”์ƒˆ๋Š” ๋˜ ํ˜‘์—…๋„ ํ˜‘์—…์ด์ง€๋งŒ ์„ค๊ณ„๋ฅผ ์–ด๋–ป๊ฒŒ ํ•˜๋Š๋ƒ๋„ ๋”ฐ์ง€๋”๊ตฐ์š”.

image
์˜์™ธ๋กœ ์‹œ๊ฐ„์€ ๋ณ„๋กœ ๋ฌธ์ œ๊ฐ€ ์—†์—ˆ๋„ค์š”..
์ตœ๋Œ€ 20 * 20 ๋ฐฐ์—ด์ธ ๊ฒฝ์šฐ ๊ฐ ์ค„ ๋ณ„๋กœ ์ˆœ๋ฐฉํ–ฅ 20๋ฒˆ, ์—ญ๋ฐฉํ–ฅ 20 * 21 / 2๋ฒˆ ์”ฉ 20๊ฐœ์˜ ์ค„์„ ์—ฐ์‚ฐํ•ด์•ผํ•˜๊ณ  ์ด๊ฑธ ์ตœ์•…์˜ ๊ฒฝ์šฐ 4^5๋ฒˆ ์—ฐ์‚ฐํ•ด์•ผํ•˜๋Š”๋ฐ.. ์•„๋งˆ ์˜ˆ์ƒ์ปจ๋ฐ ๋” ์ด์ƒ ์›€์ง์ž„์ด ์—†๋‹ค ํŒ๋‹จ๋˜๋ฉด ์žฌ๊ท€๋ฅผ ๋ฉˆ์ถ”๊ฒŒ ๋ฐฑํŠธ๋ž˜ํ‚น์„ ์‹œ์ผœ๋‘ฌ์„œ ๊ทธ๊ฒŒ ํšจ๊ณผ์ ์œผ๋กœ ์ž‘๋™ํ•˜์ง€ ์•Š๋‚˜ ์‹ถ๋„ค์š” ใ…Žใ…Ž

Copy link
Collaborator

@seongwon030 seongwon030 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ƒํ•˜์ขŒ์šฐ 4๋ฒˆ์— ์ตœ๋Œ€ 5๋ฒˆ์ด๋‹ˆ๊นŒ 4์˜ 5์Šนํ•ด์„œ 1024๋ฒˆ์ด ์ตœ๋Œ€๋กœ ์›€์ง์ด๋Š” ํšŸ์ˆ˜๊ฐ€ ๋˜๊ฒ ๋„ค์š”. ๋ง๊ทธ๋Œ€๋กœ ํ•˜๋‚˜ํ•˜๋‚˜ ๊ตฌํ˜„ํ•˜๋Š” ๋ฌธ์ œ์˜€๊ตฐ์š”. ์˜›๋‚ ์— ํ–ˆ๋˜ ๊ฒŒ์ž„์ด๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ๋กœ ๋‚˜์˜จ๊ฒŒ ์‹ ๊ธฐํ–ˆ์Šต๋‹ˆ๋‹ค. ํ’€์•„ ์ž˜ ๋ณด๊ณ  ๊ฐ‘๋‹ˆ๋‹ค !

@yuyu0830 yuyu0830 merged commit d280707 into main Jul 16, 2024
@yuyu0830 yuyu0830 deleted the 18-yuyu0830 branch July 16, 2024 13:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants