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

13-YIM2UL2ET #42

Merged
merged 5 commits into from
May 11, 2024
Merged

13-YIM2UL2ET #42

merged 5 commits into from
May 11, 2024

Conversation

YIM2UL2ET
Copy link
Collaborator

@YIM2UL2ET YIM2UL2ET commented Mar 29, 2024

πŸ”— 문제 링크

BOJ 7569 - ν† λ§ˆν† 

βœ”οΈ μ†Œμš”λœ μ‹œκ°„

μ•½ 3h

✨ μˆ˜λ„ μ½”λ“œ

1. 문제 μ„€λͺ…

μ•ˆμ΅μ€ ν† λ§ˆν†  0, 읡은 ν† λ§ˆν†  1, λΉ„μ–΄μžˆλŠ” μΉΈ -1을 3차원 배열에 μž…λ ₯ λ°›μ•„ ν•˜λ£¨λ§ˆλ‹€ 1의 μ•žλ’€μƒν•˜μ’Œμš° 0이던 칸이 1이 λ˜μ–΄ 0μ΄μ—ˆλ˜ μΉΈ 전체λ₯Ό 1둜 λ°”κΎΈλŠ” 데 μ–Όλ§ˆμ˜ μ‹œκ°„μ΄ μ†Œμš”λ˜λŠ” 지λ₯Ό κ΅¬ν•˜λŠ” 문제

2. 풀이 방식

풀이 방법과 λ³€μˆ˜

ν•˜λ£¨μ— λ²”μœ„λ₯Ό 점점 λ„“ν˜€κ°€λŠ” 방식. BFSλ₯Ό μ‚¬μš©ν•œλ‹€.
non = 읡지 μ•Šμ€ ν† λ§ˆν†  개수, res = 카운트 ν•œ 일수
νλŠ” curQueue λ³€μˆ˜λ₯Ό μ΄μš©ν•˜μ—¬ true, false λ°”κΏ”κ°€λ©° μ΄μ€‘μœΌλ‘œ μ‚¬μš©ν•œλ‹€.

main

  1. λ¨Όμ € 쒌우 길이 n, μ•žλ’€ 길이 m, 높이h λ₯Ό μž…λ ₯ λ°›κ³ , 그에 맞게 3차원 배열을 선언함.
  2. 3차원 λ°°μ—΄ μš”μ†Œλ₯Ό μž…λ ₯λ°›κΈ° μ‹œμž‘ν•œλ‹€. μ΄λ•Œ μž…λ ₯을 λ°›κ³ , κ·Έ 값이
    if 0이면 읡지 μ•Šμ•˜λ‹€λŠ” 뜻의 λ³€μˆ˜ non을 +1 ν•΄μ€Œ.
    if 1이면 bfs에 μ‚¬μš©ν•  μ’Œν‘œμ΄λ―€λ‘œ 큐에 push
  3. BFSλ₯Ό μ‚¬μš©ν•˜μ—¬ 닡을 κ΅¬ν•œλ‹€.

BFS

while (큐가 λΉ„μ–΄μžˆκ³ , `non`이 λ‚¨μ•„μžˆμœΌλ©΄)  {
    while (큐가 λ‚¨μ•„μžˆμ„ λ•Œ κΉŒμ§€) {
        1. 큐의 `front`에 μžˆλŠ” `μ’Œν‘œ`λ₯Ό `x`, `y`, `z`에 μ €μž₯
        2. 각각에 μ˜€ν”„μ…‹μ— 따라 배열을 λ‚˜κ°€λŠ”μ§€ 확인함
            if μ•ˆλ‚˜κ°„λ‹€λ©΄ μ˜€ν”„μ…‹μ„ λ”ν•œ μ’Œν‘œμ˜ 칸이 `0`이면 λ‹€λ₯Έ 큐에 μ €μž₯, `non`--, ν•΄λ‹Ή μ’Œν‘œ κ°’ `1`둜 λ³€κ²½
        3. 큐 `pop` ν•˜κΈ°
        }
    `curQueue` λ³€μˆ˜λ₯Ό `!`둜 λΆ€μ •ν•˜μ—¬ 이후에 λ‹€λ₯Έ 큐λ₯Ό μ“Έ 수 있게 함.
    `res`++ν•˜μ—¬ 일수 카운트
}

좜λ ₯

non이 λ‚¨μ•„μžˆμœΌλ©΄ 읡지 λͺ»ν•˜λŠ” ν† λ§ˆν† κ°€ μžˆλ‹€λŠ” λœ»μ΄λ―€λ‘œ -1 좜λ ₯, μ•„λ‹ˆλ©΄ res 좜λ ₯

3. μ΅œμ’… μ½”λ“œ

#include <iostream>
#include <vector>
#include <queue>

struct idx {int z, y, x;};

int main(void)
{
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);

    int m, n, h, res = 0, non = 0;
    std::vector <idx> offset = {{1, 0, 0}, {-1, 0, 0}, {0, 1, 0}, {0, -1, 0}, {0, 0, 1}, {0, 0, -1}};
    std::queue <idx> que[2];
    bool curQueue = false;

    std::cin >> n >> m >> h;
    int box[h][m][n];

    for (int z = 0; z < h; z++) {
        for (int y = 0; y < m; y++) {
            for (int x = 0; x < n; x++) {
                std::cin >> box[z][y][x];
                if (box[z][y][x] == 1) que[curQueue].push({z, y, x});
                else if (box[z][y][x] == 0) non++;
            }
        }
    }

    while (!que[curQueue].empty() && non > 0) {
        while (!que[curQueue].empty()) {
            int z = que[curQueue].front().z, y = que[curQueue].front().y, x = que[curQueue].front().x;
            for (idx set : offset) {
                int zz = z + set.z, yy = y + set.y, xx = x + set.x;
                if (zz >= 0 && zz < h && yy >= 0 && yy < m && xx >= 0 && xx < n && box[zz][yy][xx] == 0) {
                    box[zz][yy][xx] = 1, non--;
                    que[!curQueue].push({zz, yy, xx});
                }
            }
            que[curQueue].pop();
        }
        curQueue = !curQueue;
        res++;
    }

    if (non > 0) std::cout << -1;
    else std::cout << res;
    
    return 0;
}

πŸ“š μƒˆλ‘­κ²Œ μ•Œκ²Œλœ λ‚΄μš©

  1. BFS ν…Œν¬λ‹‰ (feat. ꡐ황씨)
    배열에 탐색할 μ’Œν‘œ μ˜€ν”„μ…‹μ„ λ„£κ³  for문으둜 돌리면 μ½”λ“œκ°€ 훨씬 간결해짐
std::vector <idx> offset = {{1, 0, 0}, {-1, 0, 0}, {0, 1, 0}, {0, -1, 0}, {0, 0, 1}, {0, 0, -1}};
// for (idx set : offset) {...
  1. λ°°μ—΄ 두 개 λ²ˆκ°ˆμ•„ μ“Έ 수 μžˆλŠ” ν…Œν¬λ‹‰ (feat. 상원씨)
    boolν˜• μ—°μ‚°μžλ‘œ true, false λ²ˆκ°ˆμ•„μ„œ μ“°λ©΄ μ’‹μŒ.
curQueue = !curQueue;
  1. λ°°μ—΄ for문으둜 돌릴 λ•Œ ν–‰, μ—΄, λ©΄ 헷깔리지 μ•ŠκΈ° (이것 λ•Œλ¬Έμ— μ‹œκ°„ μ—„μ²­ νƒœμ›€)
for (int z = 0; z < h; z++) {
    for (int y = 0; y < m; y++) {
        for (int x = 0; x < n; x++) {
            // code
        }
    }
}

μ§€κΈˆ μƒκ°ν•΄λ³΄λ‹ˆ μ•„μ‰¬μš΄ 점: 1μ΄μ—ˆλ˜ 칸은 bfs λŒλ¦΄λ•Œ NULL둜 λ°”κΏ”λ†“μœΌλ©΄ 큐 ν•˜λ‚˜λ‘œ ν’€κΈ° κ°€λŠ₯ν–ˆμ„λ“―.

Copy link
Collaborator

@mong3125 mong3125 left a comment

Choose a reason for hiding this comment

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

문제 μ’‹μ•„μš”!!

쒋은 문제λ₯Ό μ†Œκ°œν•΄μ£Όμ…”μ„œ μ’‹μ•˜μŠ΅λ‹ˆλ‹€. BFS의 κΈ°λ³Έ κ°œλ…μ„ μ•Œλ©΄ ν’€ 수 μžˆμœΌλ©΄μ„œλ„ 3μ°¨μ›μ΄λ‚˜ 횟수λ₯Ό μš”κ΅¬ν•΄μ„œ μ½”λ”©ν•˜λŠ” λŠ₯λ ₯을 κΈ°λ₯Ό 수 μžˆλŠ” λ¬Έμ œμ˜€λ„€μš”. 저도 직접 μˆœνšŒν•˜λŠ” 횟수λ₯Ό μ„Έμ—ˆλŠ”λ° μ•ˆ μ„ΈλŠ” 방법도 μžˆμ–΄μ„œ μ•„λž˜ μ„€λͺ… λΆ™μ΄κ² μŠ΅λ‹ˆλ‹€.

ν›„κΈ°

  1. 큐λ₯Ό λ°°μ—΄λ‘œ 두고 μ„ νƒμ μœΌλ‘œ μ‚¬μš©ν•˜λŠ” 방법은 처음 λ³΄λ„€μš”.
    큐가 λΉŒλ•ŒκΉŒμ§€ while을 λŒλ €μ•Όν•˜λŠ”λ° 쀑간에 λ”ν•΄μ£ΌλŠ”κ²Œ λ°©ν•΄κ°€ λ˜μ–΄μ„œ κ·ΈλŸ°κ°€ λ³΄λ„€μš”.
    μ „ 큐λ₯Ό 두 개 두고 λ²ˆκ°ˆμ•„κ°€λ©° μ‚¬μš©ν–ˆλŠ”λ° μŠΉν˜Έλ‹˜μ΄ ν‘Έμ‹  방법이 더 μ’‹μ•„λ³΄μ΄λ„€μš”. μƒˆλ‘œ λ°°μ›Œκ°‘λ‹ˆλ‹€~

  2. 저도 x,y,z μΆ• λ•Œλ¬Έμ— 였λ₯˜κ°€ λ‚˜λ˜λ°μš”. z,y,x둜 μˆœμ„œλ₯Ό 항상 두고 ν’€μ—ˆμŠ΅λ‹ˆλ‹€.

p.s.

BFS둜 ν‘ΈλŠ” 방법은 res 같은 λ³€μˆ˜λ₯Ό 두지 μ•Šκ³  ν† λ§ˆν†  λ°•μŠ€ λ°°μ—΄μ—μ„œ μƒˆλ‘œ ν† λ§ˆν† λ₯Ό μ΅νžλ•Œ 이전 ν† λ§ˆν† μ˜ 수 + 1둜 읡히고 λ‚˜μ€‘μ— 좜λ ₯ν•  λ•Œ, μ „μ²΄μ—μ„œ κ°€μž₯ 큰 수λ₯Ό μ°ΎλŠ” λ°©λ²•μž…λ‹ˆλ‹€. κ°€λ Ή x의 μœ„μΉ˜μ— ν† λ§ˆν† λŠ” 1μΌλ•Œ x+1 은 2이고 x+2λŠ” 3κ² μ£ ? 그러면 μ΅œμ’…μ μœΌλ‘œ 처음 ν† λ§ˆν† λŠ” (3 -1)일 μˆ™μ„±λ˜μ—ˆλ‹€κ³  λ³΄λŠ”κ±°μ£ . μ„€λͺ…이 잘 λ˜μ—ˆμ„μ§€ λͺ¨λ₯΄κ² μŠ΅λ‹ˆλ‹€.

@mong3125 mong3125 mentioned this pull request Apr 11, 2024
Copy link
Collaborator

@rivkms rivkms left a comment

Choose a reason for hiding this comment

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

μ˜ˆμ „μ— λ‹¨μˆœνžˆ 2차원 λ°°μ—΄λ‘œλ§Œ λ˜μ–΄μžˆλŠ” ν† λ§ˆν†  문제λ₯Ό ν’€μ–΄λ³΄μ•˜λŠ”λ°, 이 λ¬Έμ œλŠ” 더 μ–΄λ €μš΄ ν† λ§ˆν†  λ¬Έμ œμ΄κ΅°μš”!!
κ°œλ…μ€ λΉ„μŠ·ν•˜μ—¬ BFS둜 ν•˜μ—¬ λΉ„μŠ·ν•œ sudo codeκ°€ λ‚˜μ˜¨ 것 κ°™μ§€λ§Œ, μ½”λ“œλ₯Ό μž‘μ„±ν•  λ•Œ μžˆμ–΄ λ‹€μ–‘ν•œ ν…Œν¬λ‹‰λ“€μ΄ λ³΄μ΄λŠ”κ²ƒ κ°™μ•„ μΈμƒμ μ΄μ—ˆμŠ΅λ‹ˆλ‹€.
μƒˆλ‘œ μ•Œκ²Œ 된 λ‚΄μš©μ— μž‘μ„±ν•΄μ£Όμ‹  λ‚΄μš© λ˜ν•œ 저도 많이 배우고 μ°Έκ³ ν•  수 μžˆμ—ˆμŠ΅λ‹ˆλ‹€

μˆ˜κ³ ν•˜μ…¨μŠ΅λ‹ˆλ‹€ 😁

@YIM2UL2ET YIM2UL2ET merged commit 46272a7 into main May 11, 2024
1 check passed
@YIM2UL2ET YIM2UL2ET deleted the 13-YIM2UL2ET branch May 11, 2024 03:45
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.

3 participants