문제링크 : https://www.acmicpc.net/problem/1261

 

1261번: 알고스팟

첫째 줄에 미로의 크기를 나타내는 가로 크기 M, 세로 크기 N (1 ≤ N, M ≤ 100)이 주어진다. 다음 N개의 줄에는 미로의 상태를 나타내는 숫자 0과 1이 주어진다. 0은 빈 방을 의미하고, 1은 벽을 의미한다. (1, 1)과 (N, M)은 항상 뚫려있다.

www.acmicpc.net

 

 문제 설명 

 

1. 배열의 크기 N,M이주어짐

2. 현재 위치 (1,1) 에서 (N,M)까지 가는데 0은 그냥 이동 할 수있고 1은 벽이 세워져 있는곳이다.

3. 목적지 까지 도달하기 위해서는 최소 몇개의 벽을 부셔야 할까 ?

 

 알고리즘 

 

1. 먼저 입력이 띄어서 입력 받지 않기 때문에 scnaf("%1d",&map[i][j]) 를 이용하여 입력받아야됨

2. (1,1) 에서 모든 정점까지 이동하는데 드는 최소 비용을 갱신 .

   벽이 세워져있는곳은 1을 추가 하고 벽이없는곳은 값을 추가하지않는다 .

3.마지막에 map[N][M] 을출력 .

 

 코드 

 

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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
 
 
 
using namespace std;
 
typedef pair<intint> P;
typedef pair<int,P> PP; //(dist,y,x)
 
int N, M;
int map[105][105];
int dx[] = { 1,-1,0,0 };
int dy[] = { 0,0,1,-1 };
int check[105][105];
 
 
//다익스트라
void dijkstra(int y, int x) {
    check[y][x] = 0;
    priority_queue<PP> pq;
    pq.push(PP(0, P(y, x)));   //(dist,y,x)
    while (!pq.empty()) {
        int curr_y = pq.top().second.first;
        int curr_x= pq.top().second.second;
        //종류 부
        if (curr_y == N - 1 && curr_x == M - 1)break;
        int dis = -pq.top().first;
        pq.pop();
 
        //거리가 최소값이아니면 continue
        if (check[curr_y][curr_x] < dis)continue;
 
        for (int i = 0; i < 4; i++) {
            int next_x = curr_x + dx[i];
            int next_y = curr_y + dy[i];
            if (next_x >= 0 && next_x < M && next_y >= 0 && next_y < N) {
                int next_dis = dis + map[next_y][next_x];
                if (next_dis < check[next_y][next_x]) {
                    check[next_y][next_x] = next_dis;
                    pq.push(PP(-next_dis,P (next_y, next_x)));
                }
            }
                        
        }
    }
 
    return;
 
}
 
 
int main() {
 
    scanf("%d%d"&M, &N);
 
    //배열 입력
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
            scanf("%1d"&map[i][j]);
            //최대값으로 초기화.
            check[i][j] = 10001;
        }
    }
    dijkstra(00);
 
    printf("%d", check[N-1][M-1]);
        
}
cs

'c++ > 백준' 카테고리의 다른 글

백준 1504번 : 특정한 최단 경로  (0) 2020.04.30
백준 1753번: 최단 경로  (0) 2020.04.30
백준 4485번 : 녹색 옷 입은 애가 젤다지?  (0) 2020.04.30
백준 2004번 : 조합 0의 갯수  (0) 2020.04.30
백준 2458번 : 키 순서  (0) 2020.04.30
ariz1623