๐Ÿ“ Coding Test Study/Algorithm Problem

[C++][BAEKJOON][Dijkstra Alg] 1261๋ฒˆ ์•Œ๊ณ ์ŠคํŒŸ

ibelieveinme 2021. 8. 22. 16:42
728x90

๋ฌธ์ œ

https://www.acmicpc.net/problem/1261

 

1261๋ฒˆ: ์•Œ๊ณ ์ŠคํŒŸ

์ฒซ์งธ ์ค„์— ๋ฏธ๋กœ์˜ ํฌ๊ธฐ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฐ€๋กœ ํฌ๊ธฐ M, ์„ธ๋กœ ํฌ๊ธฐ N (1 ≤ N, M ≤ 100)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‹ค์Œ N๊ฐœ์˜ ์ค„์—๋Š” ๋ฏธ๋กœ์˜ ์ƒํƒœ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ˆซ์ž 0๊ณผ 1์ด ์ฃผ์–ด์ง„๋‹ค. 0์€ ๋นˆ ๋ฐฉ์„ ์˜๋ฏธํ•˜๊ณ , 1์€ ๋ฒฝ์„ ์˜๋ฏธ

www.acmicpc.net

 

์ฝ”๋“œ

#include <iostream>
#include <queue>
using namespace std;

void dijkstra();

int M, N;//M: ์—ด, N: ํ–‰
int map[101][101];//๋ฌธ์ œ์—์„œ ์ฃผ์–ด์ง„ map์ •๋ณด
int cost[101][101];//map์˜ ๋น„์šฉ์„ ์ €์žฅํ•  ๋ฐฐ์—ด
int direction[4][2] = { {0,1},{1,0},{0,-1},{-1,0} };//์˜ค๋ฅธ์ชฝ, ์•„๋ž˜, ์™ผ์ชฝ, ์œ„
const int INF = 987654321;

int main() {
    cin >> M >> N;//โ˜…โ˜…โ˜…์ฃผ์˜: ํ–‰, ์—ด์ด ์•„๋‹ˆ๋ผ ์—ด, ํ–‰์œผ๋กœ ๋ฐ›์•„์ง!
    for (int n = 0; n < N; n++) {//ํ–‰
        for (int m = 0; m < M; m++) {//์—ด
            scanf_s("%1d", &map[n][m]);
            cost[n][m] = INF;//๋ชจ๋“  ์ž๋ฆฌ์˜ ๋น„์šฉ์„ INF๋กœ ์ดˆ๊ธฐํ™”ํ•ด๋†“๊ณ  ์ตœ์†Œ๋น„์šฉ ์ฐพ์„๊ฑฐ์ž„.
        }
    }
    dijkstra();
    cout << cost[N - 1][M - 1];//๋ฏธ๋กœ์˜ ๋์ธ Nํ–‰ M์—ด ๊ฐ’ ์ถœ๋ ฅ
}

void dijkstra() {
    queue<pair<int, int>> q;//BFS ํƒ์ƒ‰์„ ์œ„ํ•œ queue. x,y์ขŒํ‘œ๋ฅผ ๋‹ด์•„์•ผ ํ•˜๋ฏ€๋กœ pair_queue ์‚ฌ์šฉ.

    q.push(make_pair(0, 0));//ํƒ์ƒ‰์€ (0,0)์ขŒํ‘œ์—์„œ ์‹œ์ž‘
    cost[0][0] = 0;//(0,0)์ขŒํ‘œ ๋น„์šฉ์€ 0์ด์ง€

    while (!q.empty()) {
        int cur_x = q.front().first;//x
        int cur_y = q.front().second;//y
        q.pop();

        for (int i = 0; i < 4; i++) {
            int next_x = cur_x + direction[i][0];//๋‹ค์Œx
            int next_y = cur_y + direction[i][1];//๋‹ค์Œy

            //๋‹ค์Œ์— ํƒ์ƒ‰ํ•˜๋ ค๋Š” ์žฅ์†Œ๊ฐ€ ๋งต์˜ ๋ฒ”์œ„๋ฅผ ๋„˜์–ด์„œ๋ฉด ๋‹ค๋ฅธ ๋ฐฉํ–ฅ ํƒ์ƒ‰
            if (next_x >= N || next_y >= M || next_x < 0 || next_y < 0) continue;

            //๋‹ค์Œ์— ํƒ์ƒ‰ํ•˜๋ ค๋Š” ์žฅ์†Œ๊ฐ€ ๋ฒฝ์ผ ๋•Œ, ๋ฒฝ์„ ๋šซ๊ณ  ์ง€๋‚˜๊ฐ€๋Š” ๋น„์šฉ์€ 1
            if (map[next_x][next_y] == 1) {
                //ํ˜„์žฌ๊นŒ์ง€์˜ ๋น„์šฉ์— ๋ฒฝ์˜ ๋น„์šฉ(1)์„ ๋”ํ•œ ํ•œ ๊ฐ’๊ณผ ๋‹ค์Œ ์žฅ์†Œ์˜ ๋น„์šฉ์„ ๋น„๊ตํ•œ๋‹ค.
                //"ํ˜„์žฌ๊นŒ์ง€์˜ ๋น„์šฉ + ๋ฒฝ์˜ ๋น„์šฉ(1) > ๋‹ค์Œ ์žฅ์†Œ๊ฐ€ ๊ฐ€์ง„ ๋น„์šฉ" ์ด๋ผ๋ฉด
                //๋‹ค์Œ ์žฅ์†Œ๋Š” INF ๊ฐ’์ด๊ฑฐ๋‚˜ ๋” ํฐ ๋น„์šฉ์ด ๋“ค์–ด ์žˆ๋˜ ๊ฒƒ์ด๋ฏ€๋กœ
                //๋‹ค์Œ ์žฅ์†Œ์˜ ๋น„์šฉ์„ ๋” ์ž‘์€ ๊ฐ’์ธ "ํ˜„์žฌ๊นŒ์ง€์˜ ๋น„์šฉ + ๋ฒฝ์˜ ๋น„์šฉ(1) > ๋‹ค์Œ ์žฅ์†Œ๊ฐ€ ๊ฐ€์ง„ ๋น„์šฉ"์œผ๋กœ ๊ฐฑ์‹ ํ•œ๋‹ค !
                //์ด๋ ‡๊ฒŒ ์ตœ์†Œ ๋น„์šฉ์„ ๊ณ„์† ๊ฐฑ์‹ ํ•ด์ฃผ๋‹ˆ๊นŒ ๋ฐฉ๋ฌธ์ฒดํฌ ๋ฐฐ์—ด์€ ๋”ฐ๋กœ ์•ˆ๋งŒ๋“ค์–ด๋„ ๋œ๋‹ค!
                //(๋ฐฉ๋ฌธ์•ˆํ–ˆ๋˜ ์ž๋ฆฌ์ด๋ฉด INF๊ฐ’์„ ๊ฐ–๊ณ  ์žˆ๊ฒŸ์ง€~. ๊ทธ๋ฆฌ๊ตฌ ๋ฐฉ๋ฌธํ–ˆ์–ด๋„ ๋” ์ž‘์€ ๊ฐ’์œผ๋กœ ๊ฐฑ์‹ ํ•ด์ค˜์•ผ์ง€~)
                if (cost[next_x][next_y] > cost[cur_x][cur_y] + 1) {
                    cost[next_x][next_y] = cost[cur_x][cur_y] + 1;
                    q.push(make_pair(next_x, next_y));
                }
            }
            //๋‹ค์Œ์— ํƒ์ƒ‰ํ•˜๋ ค๋Š” ์žฅ์†Œ๊ฐ€ ๋ฒฝ์ด ์•„๋‹ ๋•Œ, ์ง€๋‚˜๊ฐ€๋Š” ๋น„์šฉ์€ 0
            else if (map[next_x][next_y] == 0) {
                if (cost[next_x][next_y] > cost[cur_x][cur_y]) {
                    cost[next_x][next_y] = cost[cur_x][cur_y];
                    q.push(make_pair(next_x, next_y));
                }
            }
        }
    }
}
728x90