Henu
개발냥발
Henu
전체 방문자
오늘
어제
  • 분류 전체보기 (411)
    • DevOps (52)
      • Kubernetes (19)
      • Docker (14)
      • AWS (3)
      • Nginx (4)
      • Linux (4)
      • ArgoCD (1)
      • CN (2)
      • NATS (0)
      • Git (5)
    • Back-End (30)
      • Django (18)
      • Spring (5)
      • JPA (1)
      • MSA (5)
    • CS (87)
      • SystemSoftware (20)
      • OS (25)
      • Computer Architecture (16)
      • Network (23)
      • Database (2)
    • Lang (21)
      • Java (9)
      • Python (4)
      • C# (8)
    • Life (12)
    • 블록체인 (2)
    • Algorithm (204)
      • BOJ (160)
      • 프로그래머스 (19)
      • LeetCode (4)
      • SWEA (1)
      • 알고리즘 문제 해결 전략 (8)
      • DS, algorithms (7)
      • Checkio (5)
    • IT (2)

블로그 메뉴

  • GitHub
  • 글쓰기
  • 관리자

공지사항

  • Free!

인기 글

태그

  • django
  • 다이나믹 프로그래밍
  • BFS
  • Kubernetes
  • 백트래킹
  • 프로그래머스
  • DFS
  • Network
  • docker
  • boj

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
Henu

개발냥발

Algorithm/BOJ

[백준 2479] 경로찾기 (C++)

2021. 9. 29. 14:17
 

2479번: 경로 찾기

길이가 같은 두 개의 이진수 코드 A와 B가 있다고 하자. 이 두 코드 사이의 해밍 거리는 A와 B의 각 비트를 왼쪽부터 오른쪽으로 차례대로 비교할 때 서로 다른 값을 가진 비트의 수이다. 예를 들

www.acmicpc.net

 

해밍 거리가 1인 경우만 서로 이동이 가능하다.

따라서 모든 정점을 서로 1:1로 보면서(N^2) 해밍거리를 확인하고 1인 경우에 정점을 연결시켜주는 작업을 가장 먼저 한다.

 

두 번째로 가장 짧은 해밍 경로를 찾아야 하므로 A부터 B까지 최단경로를 구하는 다익스트라 알고리즘을 수행해 준다.

다익스트라 도중 우선순위 큐에 원소를 넣을 때, 큐에 들어가는 정점에 도달하기 직전의 정점이 현재 정점이므로 parent[next] = now 를 통해 이전 정점을 기록해준다.

 

B부터 부모 정점을 찾아 되돌아 가면서 답을 구할 수 있다.

 

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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <cstring>
#define fasti ios_base::sync_with_stdio(false); cin.tie(0);
#define fastio ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define INF 1e9+7
#define pii pair<int, int>
 
typedef long long ll;
// typedef pair<int, int> pii;
 
using namespace std;
 
int N, K, A, B;
int arr[1001][1001];
int dist[1001];
int parent[1001];
vector<pii > graph[1001];
 
void input(){
    cin >> N >> K;
    string str;
    for(int i = 0; i < N; i++){
        cin >> str;
        for(int j = 0; j < K; j++){
            arr[i][j] = str[j];
        }
    }
    
    cin >> A >> B;
}
 
int HammingDist(int u, int v){
    int d = 0;
    for(int i = 0; i < K; i++){
        if(arr[u-1][i] != arr[v-1][i]) d++;
    }
    return d;
}
 
void make_graph(){
    for(int i = 1; i <= N-1; i++){
        for(int j = i+1; j <= N; j++){
            int dist = HammingDist(i, j);
            if(dist == 1){
                graph[i].push_back({dist, j});
                graph[j].push_back({dist, i});
            }
        }
    }
}
 
bool Dijkstra(){
    for(int i = 0; i < 1001; i++){
        dist[i] = INF;
    }
    
    priority_queue<pii, vector<pii >, greater<pii > > pq;
    pq.push({0, A});
    dist[A] = 0;
    
    while(!pq.empty()){
        int now = pq.top().second;
        int now_dist = pq.top().first;
        pq.pop();
        
        if(now == B) return true;
        
        for(int i = 0; i < graph[now].size(); i++){
            int next = graph[now][i].second;
            int next_dist = graph[now][i].first + now_dist;
            
            if(next_dist < dist[next]){
                parent[next] = now;
                dist[next] = next_dist;
                pq.push({next_dist, next});
            }
        }
    }
    
    return false;
}
 
void solve(){
    make_graph();
    bool isfound = Dijkstra();
    
    if(!isfound){
        cout << -1;
        return;
    }
    
    vector<int> path;
    path.push_back(B);
    for(int p = parent[B]; p != A; p = parent[p]){
        path.push_back(p);
    }
    path.push_back(A);
    
    for(int i = path.size()-1; i >= 0; i--){
        cout << path[i] << " ";
    }
}
 
int main(){
    fastio
    input();
    solve();
    
    return 0;
}
 
 
Colored by Color Scripter
cs

 

'Algorithm > BOJ' 카테고리의 다른 글

[백준12762 ] 롤러코스터 (C++)  (0) 2021.10.06
[백준 1234] 크리스마스 트리 (C++)  (0) 2021.10.06
[백준 16437] 양 구출 작전 (C++)  (0) 2021.09.28
[백준 15591] MooTube(Silver) (C++)  (0) 2021.09.28
[백준 3980] 선발 명단 (C++)  (0) 2021.09.15
    'Algorithm/BOJ' 카테고리의 다른 글
    • [백준12762 ] 롤러코스터 (C++)
    • [백준 1234] 크리스마스 트리 (C++)
    • [백준 16437] 양 구출 작전 (C++)
    • [백준 15591] MooTube(Silver) (C++)

    티스토리툴바