[BOJ] 16928๋ฒˆ | ๋ฑ€๊ณผ ์‚ฌ๋‹ค๋ฆฌ ๊ฒŒ์ž„ (C++)

2023. 12. 1. 18:11ใ†Coding Test/BOJ

๐Ÿ”—๋ฌธ์ œ ๋ณด๋Ÿฌ๊ฐ€๊ธฐ
 

16928๋ฒˆ: ๋ฑ€๊ณผ ์‚ฌ๋‹ค๋ฆฌ ๊ฒŒ์ž„

์ฒซ์งธ ์ค„์— ๊ฒŒ์ž„ํŒ์— ์žˆ๋Š” ์‚ฌ๋‹ค๋ฆฌ์˜ ์ˆ˜ N(1 ≤ N ≤ 15)๊ณผ ๋ฑ€์˜ ์ˆ˜ M(1 ≤ M ≤ 15)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์—๋Š” ์‚ฌ๋‹ค๋ฆฌ์˜ ์ •๋ณด๋ฅผ ์˜๋ฏธํ•˜๋Š” x, y (x < y)๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. x๋ฒˆ ์นธ์— ๋„์ฐฉํ•˜๋ฉด, y๋ฒˆ ์นธ์œผ

www.acmicpc.net

 

 

๐Ÿง‘‍๐Ÿ’ปํ’€์ด ๊ณผ์ •

 

BFS๋กœ ์‰ฝ๊ฒŒ ํ’€ ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ์ธ๋ฐ, ์ €๋Š” ์‚ฌ๋‹ค๋ฆฌ๋‚˜ ๋ฑ€์„ ํƒ€๊ณ  ๊ฐ„ ๋ณด๋“œ์นธ์„ ๋ฐฉ๋ฌธ ์ฒ˜๋ฆฌํ•ด์ฃผ์งˆ ์•Š์•„, ์—‰๋šฑํ•œ ๋‹ต์ด ๋‚˜์™”์Šต๋‹ˆ๋‹ค. ์ด๊ฑธ ๋นจ๋ฆฌ ์•Œ์•„์ฑ„์ง€ ๋ชปํ•ด์„œ ์‹œ๊ฐ„์„ ์ข€ ํ—ˆ๋น„ํ–ˆ๋„ค์š”. ์•„๋ฌดํŠผ, ์ œ๊ฐ€ ํ‘ผ ํ’€์ด ๊ณผ์ •์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

  1. ๋ฑ€์ด๋‚˜ ์‚ฌ๋‹ค๋ฆฌ ์ •๋ณด๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” vector<int> teleports(100 + 1)์„ ์ค€๋น„ํ•œ๋‹ค.
    • teleports[i] = 0   ๐Ÿ‘‰๐Ÿป ๋ฑ€์ด๋‚˜ ์‚ฌ๋‹ค๋ฆฌ๊ฐ€ ์—†์Œ์„ ์˜๋ฏธ
    • teleports[i] != 0  ๐Ÿ‘‰๐Ÿป ๋ฑ€์ด๋‚˜ ์‚ฌ๋‹ค๋ฆฌ๋ฅผ ํƒ€๊ณ  ๊ฐˆ ๋‹ค์Œ ์ธ๋ฑ์Šค
  2. ์ฃผ์‚ฌ์œ„๋ฅผ ๊ตด๋ฆฐ ํšŸ์ˆ˜ ์ •๋ณด๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” vector<int> diceCounts(100 + 1)์„ ์ค€๋น„ํ•œ๋‹ค.
  3. 1๋ฒˆ ์œ„์น˜๋ถ€ํ„ฐ BFS ํƒ์ƒ‰์„ ์‹œ์ž‘ํ•œ๋‹ค.
    • ํ˜„์žฌ ์œ„์น˜(current)๋กœ๋ถ€ํ„ฐ 1 ~ 6์„ ๋”ํ•œ ๋‹ค์Œ ์œ„์น˜ ๊ฐ’(next)์„ ์–ป๋Š”๋‹ค.
      • ๋ณด๋“œ๊ฒŒ์ž„ ์นธ ๋ฒ”์œ„๋ฅผ ๋„˜์–ด๊ฐ€๊ฑฐ๋‚˜ ์ด๋ฏธ ๋ฐฉ๋ฌธ(diceCounts[next] != 0)ํ•œ ๊ฒฝ์šฐ, continue
      • next == 100์ด๋ผ๋ฉด, diceCounts[current] + 1์„ ๋ฆฌํ„ด
      • ๋ฑ€์ด๋‚˜ ์‚ฌ๋‹ค๋ฆฌ๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ(teleports[next] != 0), next๋ฅผ ๊ฐฑ์‹ 
      • ๋ฑ€์ด๋‚˜ ์‚ฌ๋‹ค๋ฆฌ๋ฅผ ํƒ€๊ณ  ๊ฐ„ ๊ณณ์ด ์ด๋ฏธ ๋ฐฉ๋ฌธํ•œ ๊ณณ(diceCounts[next] != 0)์ธ ๊ฒฝ์šฐ, continue
      • diceCounts[next] = diceCounts[current] + 1์„ ํ•˜๊ณ , ํ์— next ์‚ฝ์ž…

 

โœ๏ธ์†Œ์Šค ์ฝ”๋“œ ๋ฐ ๊ฒฐ๊ณผ

#include <iostream>
#include <vector>
#include <queue>
using namespace std;
#define FAST_IO ios::sync_with_stdio(false); cout.tie(NULL); cin.tie(NULL);

const int BOARD_SIZE = 100;

int BFS(vector<int>& teleports, vector<int>& diceCounts) {
    queue<int> indexes;
    indexes.push(1);

    while (!indexes.empty()) {
        int current = indexes.front();
        indexes.pop();

        for (int i = 1; i <= 6; i++) {
            int next = current + i;

            if (next > BOARD_SIZE or diceCounts[next] != 0)
                continue;
            
            if (next == BOARD_SIZE)
                return diceCounts[current] + 1;

            while (teleports[next] != 0)
                next = teleports[next];

            if (diceCounts[next] != 0)
                continue;

            diceCounts[next] = diceCounts[current] + 1;
            indexes.emplace(next);
        }
    }

    return 0;
}


void AddTeleportInformations(vector<int>& next, int N) {
    int from, to;

    for (int i = 0; i < N; i++) {
        cin >> from >> to;
        next[from] = to;
    }
}

int main() {
    FAST_IO

    // 1. ์ดˆ๊ธฐํ™”
    vector<int> teleports(BOARD_SIZE + 1, 0);
    vector<int> diceCounts(BOARD_SIZE + 1, 0);

    // 2. ์‚ฌ๋‹ค๋ฆฌ๋ž‘ ๋ฑ€ ์š”์†Œ ์ถ”๊ฐ€
    int ladderCount, snakeCount;
    cin >> ladderCount >> snakeCount;

    AddTeleportInformations(teleports, ladderCount);
    AddTeleportInformations(teleports, snakeCount);

    // 3. BFS ํƒ์ƒ‰ ์‹œ์ž‘
    int answer = BFS(teleports, diceCounts);
    cout << answer;
    return 0;
}

 

 

 

728x90
๋ฐ˜์‘ํ˜•