๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ”ฅ PS(Problem Solving) ๐Ÿ”ฅ/goorm

[๊ตฌ๋ฆ„LEVEL] ๋ฒฝ ํ†ต๊ณผํ•˜๊ธฐ

by dar0m! 2020. 3. 25.
๋‚œ์ด๋„ ์ •๋‹ต๋ฅ 
โ˜…โ˜…โ˜…โ˜… -%

ํ”„๋ฆฌ๋ฏธ์—„ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์œ„ํด๋ฆฌ ๋น„ํƒ€์•Œ๊ณ  ์‹œ์ฆŒ3 4์ฃผ์ฐจ

 

goorm

๊ตฌ๋ฆ„์€ ํด๋ผ์šฐ๋“œ ๊ธฐ์ˆ ์„ ์ด์šฉํ•˜์—ฌ ๋ˆ„๊ตฌ๋‚˜ ์ฝ”๋”ฉ์„ ๋ฐฐ์šฐ๊ณ , ์‹ค๋ ฅ์„ ํ‰๊ฐ€ํ•˜๊ณ , ์†Œํ”„ํŠธ์›จ์–ด๋ฅผ ๊ฐœ๋ฐœํ•  ์ˆ˜ ์žˆ๋Š” ํด๋ผ์šฐ๋“œ ์†Œํ”„ํŠธ์›จ์–ด ์ƒํƒœ๊ณ„์ž…๋‹ˆ๋‹ค.

www.goorm.io

๋ฌธ์ œ

 

ํ•ด๊ฒฐ

key point, ๋ฐฉ๋ฌธํ•œ ์‹œ๊ฐ์ด ์ž‘์€ ๊ณณ์„ ์šฐ์„ ์ ์œผ๋กœ ๋ฐฉ๋ฌธํ•œ๋‹ค. priority_queue
  1. ์šฐ์„ ์ˆœ์œ„ ํ์— ๋ฐฉ๋ฌธํ•œ ์‹œ๊ฐ๊ณผ ์ขŒํ‘œ๋ฅผ ์ €์žฅํ•œ๋‹ค. → {time, {x, y}}
  2. ํ˜„์žฌ ๋ณด๊ณ ์žˆ๋Š” ์œ„์น˜์™€ ์ธ์ ‘ํ•œ ๊ณณ์„ ํ™•์ธํ•˜๋ฉด์„œ
    1. ๋ฒฝ์ด ์žˆ๋‹ค๋ฉด ์‹œ๊ฐ„์€ ๊ทธ๋Œ€๋กœ, ์ขŒํ‘œ๋งŒ ๋‹ค์Œ ์œ„์น˜์— ๋งž์ถฐ ๋„ฃ์–ด์ค€๋‹ค.
    2. ๋ฒฝ์ด ์—†๋‹ค๋ฉด ์‹œ๊ฐ„์€ +1, ๋‹ค์Œ ์œ„์น˜ ์ขŒํ‘œ๋ฅผ ๋„ฃ์–ด์ค€๋‹ค.
  3. ์ด๋ ‡๊ฒŒ ์ˆœ์ฐจ์ ์œผ๋กœ ์ˆœํšŒํ•˜๋‹ค๋ณด๋ฉด N,N ์œ„์น˜์— ๋„๋‹ฌํ•˜๊ฒŒ ๋˜๊ณ  ๊ทธ ๋•Œ์˜ time์„ ์ถœ๋ ฅํ•˜๋ฉด ์ตœ์†Œ ์‹œ๊ฐ„์ด ๋œ๋‹ค.

 

์ฝ”๋“œ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <string.h>
#include <algorithm>
#include <queue>
using namespace std;
typedef pair<intint> p;
int n, ans = 1e9;
int dx[] = { 0,1,0-1 }, dy[] = { 1,0,-10 };
int arr[1005][1005], chk[1005][1005];
priority_queue<pair<int, p>vector<pair<int, p>>, greater<pair<int, p>>> pq;
int main() {
    scanf("%d"&n);
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            scanf("%d"&arr[i][j]);
        }
    }
    chk[0][0= 1;
    pq.push({ 1, {0,0} });
    while (!pq.empty()) {
        pair<int, p> front = pq.top();
        pq.pop();
        int time = front.first, x = front.second.first, y = front.second.second;
        if (x == n - 1 && y == n - 1) {
            printf("%d", time - 1);
            break;
        }
        for (int k = 0; k < 4; k++) {
            int nx = x + dx[k], ny = y + dy[k];
            if (nx < 0 || ny < 0 || nx >= n || ny >= n || chk[nx][ny]) continue;
            if (arr[nx][ny]) {
                chk[nx][ny] = time;
                pq.push({ time, { nx, ny } });
            }
            else {
                chk[nx][ny] = time + 1;
                pq.push({ time + 1, { nx, ny } });
            }
        }
    }
    return 0;
}
cs

 

deque์‚ฌ์šฉ

์ด์ „์— ๋ฑ์€ ์ „ํ˜€ ์‚ฌ์šฉํ•ด๋ณธ ์ ์ด ์—†์–ด์„œ ์šฐ์„ ์ˆœ์œ„ ํ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋ฌธ์ œ๋ฅผ ํ’€์—ˆ๋˜๊ฑด๋ฐ, ํ•ด์„ค์„ ํ™•์ธํ•ด๋ณด๋‹ˆ ๋ฑ์„ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์–ด์„œ ์ €๋„ ๋ฑ์„ ์ด์šฉํ•ด์„œ ๋‹ค์‹œ ํ’€์–ด๋ดค์Šต๋‹ˆ๋‹ค.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include<deque>
using namespace std;
typedef pair<intint> p;
int arr[1005][1005], chk[1005][1005];
int dx[] = { 010-1 };
int dy[] = { 10-10};
deque<p> dq;
int main() {
    int n; scanf("%d"&n);
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            scanf("%d"&arr[i][j]);
 
    dq.push_back({ 00 });
    chk[0][0= 1;
    while (dq.size()) {
        auto cur = dq.front();
        int x = cur.first, y = cur.second;
        dq.pop_front();
        if (x == n - 1 && y == n - 1) {
            printf("%d", chk[x][y] - 1);
            return 0;
        }
        for (int i = 0; i < 4++i) {
            int nx = x + dx[i], ny = y + dy[i];
            if (nx < 0 || ny < 0 || nx >= n || ny >= n || chk[nx][ny]) continue;
            if (arr[nx][ny]) {    //๋‹ค์Œ ๋ฐฉ๋ฌธํ•ด์•ผ ํ•  ๊ณณ์ด ๋ฒฝ์ด๋ผ๋ฉด ๋ฑ์˜ ์•ž์— ์ •์ ์„ ๋„ฃ์–ด์คŒ
                chk[nx][ny] = chk[x][y];
                dq.push_front({ nx, ny });
            }
            else {    //๋‹ค์Œ ๋ฐฉ๋ฌธํ•ด์•ผ ํ•  ๊ณณ์ด ๋ฒฝ์ด ์•„๋‹ˆ๋ผ๋ฉด ๋ฑ์˜ ๋’ค์— ์ •์ ์„ ๋„ฃ์–ด์คŒ
                chk[nx][ny] = chk[x][y] + 1;
                dq.push_back({ nx, ny });
            }            
        }
    }
}
cs

๋Œ“๊ธ€