[c++] BOJ 17244 :: ์•„๋งž๋‹ค์šฐ์‚ฐ

๋ฌธ์ œ

์•„๋งž๋‹ค ์šฐ์‚ฐ ๋ฌธ์ œ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๊ฒฝ์žฌ์”จ๋Š” ์ €๋… ์•ฝ์†์„ ๊ฐ€๊ธฐ ์ „ ์ฑ™๊ธฐ์ง€ ์•Š์€ ๋ฌผ๊ฑด๋“ค์ด ์žˆ๋Š” ์ง€ ํ™•์ธํ•˜๊ณ  ์žˆ๋‹ค. ํ•„์š”ํ•œ ๋ฌผ๊ฑด์€ ์ „๋ถ€ ์ฑ™๊ธด ๊ฒƒ ๊ฐ™์•˜๊ณ  ์™ธ์ถœ ํ›„ ๋Œ์•„์˜ค๋Š” ๊ธธ์— ๊ฒฝ์žฌ์”จ๋Š” ์™ธ์ณค๋‹ค.

"์•„ ๋งž๋‹ค ์šฐ์‚ฐ!!!"

๊ฒฝ์žฌ ์”จ๋Š” ๋งค๋ฒˆ ์™ธ์ถœํ•˜๊ณ  ๋‚˜์„œ์•ผ ์–ด๋–ค ๋ฌผ๊ฑด์„ ์ง‘์— ๋†“๊ณ  ์™”๋‹ค๋Š” ๊ฒƒ์„ ๋– ์˜ฌ๋ฆด ๋•Œ๋งˆ๋‹ค ์ž์ฑ…๊ฐ์— ์‹œ๋‹ฌ๋ฆฌ๋Š” ๊ฒƒ์ด ๋„ˆ๋ฌด ์‹ซ์—ˆ๋‹ค.

์™ธ์ถœ์ด ์žฆ์€ ๊ฒฝ์žฌ ์”จ๋Š” ๋ฐ˜๋ณต๋˜๋Š” ์ผ์„ ๊ทผ์ ˆํ•˜๊ธฐ ์œ„ํ•ด ๊ผญ ์ฑ™๊ฒจ์•ผ ํ•  ๋ฌผ๊ฑด๋“ค์„ ์ •๋ฆฌํ•ด๋ณด์•˜๋‹ค. ํ•˜์ง€๋งŒ ์ง€๊ฐ‘, ์Šค๋งˆํŠธํฐ, ์šฐ์‚ฐ, ์ฐจ ํ‚ค, ์ด์–ดํฐ, ์‹œ๊ณ„, ๋ณด์กฐ ๋ฐฐํ„ฐ๋ฆฌ ๋“ฑ ์ข…๋ฅ˜์™€ ๊ฐœ์ˆ˜๊ฐ€ ๋„ˆ๋ฌด ๋งŽ์•˜๋‹ค.

ํ‰์†Œ ๋ถˆํ•„์š”ํ•œ ์›€์ง์ž„์„ ์•„์ฃผ ์‹ซ์–ดํ•˜๋Š” ๊ฒฝ์žฌ ์”จ๋Š” ์ด ๋ฌผ๊ฑด๋“ค์„ ์ตœ๋Œ€ํ•œ ๋น ๋ฅด๊ฒŒ ์ฑ™๊ฒจ์„œ ์™ธ์ถœํ•˜๋Š” ์ด๋™ ๊ฒฝ๋กœ๋ฅผ ์•Œ๊ณ  ์‹ถ์—ˆ๋‹ค.

๊ฒฝ์žฌ ์”จ๋Š” ํ•œ ๊ฑธ์Œ์— ์ƒํ•˜์ขŒ์šฐ์— ์ธ์ ‘ํ•œ ์นธ์œผ๋กœ๋งŒ ์›€์ง์ผ ์ˆ˜ ์žˆ๋‹ค.

๊ฒฝ์žฌ ์”จ๋ฅผ ์œ„ํ•ด ์ง‘์„ ์œ„์—์„œ ๋ฐ”๋ผ๋ณธ ๋ชจ์Šต๊ณผ ์ฑ™๊ฒจ์•ผ ํ•  ๋ฌผ๊ฑด๋“ค์˜ ์œ„์น˜๋“ค์„ ์•Œ๊ณ  ์žˆ์„ ๋•Œ, ๋ฌผ๊ฑด์„ ๋ชจ๋‘ ์ฑ™๊ฒจ์„œ ์™ธ์ถœํ•˜๊ธฐ๊นŒ์ง€ ์ตœ์†Œ ๋ช‡ ๊ฑธ์Œ์ด ํ•„์š”ํ•œ์ง€ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์ž.

 

ํ’€์ด

  • BFS ์ˆœํšŒ ๋ฌธ์ œ
  • ๋ฉ”๋ชจ์ด์ œ์ด์…˜์„ ์–ด๋–ป๊ฒŒ ํ•  ๊ฒƒ์ธ๊ฐ€๊ฐ€ ๊ด€๊ฑด
    • ์ตœ๋Œ€ 5๊ฐœ์˜ ๋ฌผ๊ฑด์ด๋‹ˆ bitmap ์‚ฌ์šฉ

 

์ฝ”๋“œ

#include <iostream>
#include <string>
#include <queue>
#include <unordered_set>

using namespace std;

int map[51][51];
int direction[4][2] = { {1, 0}, {0, -1}, {-1, 0}, {0, 1} };
int visitedPath[51][51];
unordered_set<int> visitedProduct[51][51];
int POW[5] = { 1, 10, 100, 1000, 10000 };
vector<pair<int, int>> productList;

struct Node {
    int row;
    int col;
    int path;
    int productList = 0;
    Node(int r, int c, int p) {
        row = r; col = c; path = p;
    }
};


int main() {
    // S์—์„œ ๋ชจ๋“  X๋ฅผ ๊ฑฐ์ณ E
    fill(&visitedPath[0][0], &visitedPath[50][51], 1e9);

    int N, M;
    cin >> N >> M;

    pair<int,int> start; pair<int, int> end;
    for (int i = 0; i < M; i++) {
        string str; cin >> str;
        for (int j = 0; j < str.size(); j++) {
            map[i][j] = str[j];
            if (map[i][j] == 'S') {
                start = make_pair(i, j);
            }
            else if (map[i][j] == 'E') {
                end = make_pair(i, j);
            }
            else if (map[i][j] == 'X') {
                productList.push_back(make_pair(i, j)); // X๋ฅผ 0~4๋กœ ๋งคํ•‘
            }
        }
    }

    int result = 0;
    for (int i = 0; i < productList.size(); i++) {
        // ๋ฏธ๋ฆฌ ์ €์žฅํ•ด๋‘๊ธฐ (product ๋‹ค ์ฑ„์› ์„ ๋•Œ)
        result += POW[i];
    }

    queue<Node*> que;
    que.push(new Node(start.first, start.second, 0));
    visitedPath[start.first][start.second] = 0;

    while (!que.empty()) {
        auto pos = que.front(); que.pop();

        if (pos->row == end.first && pos->col == end.second) {
            if (pos->productList == result) {
                cout << pos->path;
                return 0;
            }
        }

        for (int i = 0; i < 4; i++) {
            int r = pos->row + direction[i][0];
            int c = pos->col + direction[i][1];

            if (r < 0 || r >= M || c < 0 || c >= N) {
                continue;
            }
            if (map[r][c] == '#') {
                continue;
            }

            Node* tmp = new Node(r, c, pos->path+1);
            tmp->productList = pos->productList;

            if (map[r][c] == 'X') {
                int idx = 0;
                for (int i = 0; i < productList.size(); i++) {
                    if (r == productList[i].first && c == productList[i].second) {
                        idx = i;
                        break;
                    }
                }
                int haveProduct = (tmp->productList / POW[idx]) % 10;
                if (haveProduct == 0) {
                    tmp->productList += POW[idx];
                }
            }
            auto sameProductAgain = (visitedProduct[r][c].find(pos->productList) != visitedProduct[r][c].end());
            if ((visitedPath[r][c] <= pos->path+1) && sameProductAgain) {
                // ๋” ํฐ path๋กœ ๋“ค๋ฅด๋Š”๋ฐ, ๋“ค๋ฅธ product๊ฐ€ ๊ฐ™์œผ๋ฉด ๊ฑด๋„ˆ๋›ฐ๊ธฐ
                continue;
            }

            que.push(tmp);
            visitedPath[r][c] = tmp->path;
            visitedProduct[r][c].insert(tmp->productList);
        }
    }
}

 

๊ฐœ์„ 

  • pow๋Š” ๋ฏธ๋ฆฌ ๊ณ„์‚ฐํ•œ ๋’ค ์‚ฌ์šฉ
  • ์ฝ”๋“œ ๋„˜ ๊ธธ์–ด์„œ ๊ฐ„๋‹จํ•˜๊ฒŒ ํ•ด์•ผํ•จ

 

๊ฒฐ๊ณผ

์•„์ด๋”” ๋ฉ”๋ชจ๋ฆฌ ์‹œ๊ฐ„ ์–ธ์–ด
gmldms784 11172 40 C++17 / ์ˆ˜์ •
๋ฐ˜์‘ํ˜•