Post

baekjoon 1697:숨바꼭질

baekjoon 1697 숨바꼭질

1697번 숨바꼭질

접근

DLRS문제 풀었을 때와 같은 방식으로 하면 될 것 같다.
visited사용해서 최초만 기록해서 중복하지 않도록하고
pair로 지금 몇초 지나서 만들어 진건지 기록한다.

코드

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
#include <iostream>
#include <vector>

using namespace std;
int main(){
  int n, k;
  cin >> n >> k;
  vector<pair<int, int>> v;
  bool visited[100001] = {false};
  v.push_back({n, 0});
  visited[n] = true;
  int i = 0;
  while(true){
    pair<int, int> x = v[i];
    if(x.first == k){
      cout << x.second << "\n";
      break;
    }
    if(x.first + 1 <= 100000 && !visited[x.first + 1]){
      v.push_back({x.first + 1, x.second+1});
      visited[x.first + 1] = true;
    }
    if(x.first - 1 >= 0 && !visited[x.first - 1]){
      v.push_back({x.first - 1, x.second+1});
      visited[x.first - 1] = true;
    }
    if(x.first * 2 <= 100000 && !visited[x.first * 2]){
      v.push_back({x.first*2, x.second+1});
      visited[x.first * 2] = true;
    }
    i++;
  }
  
  
  return 0;
}

배운 점

없는 듯. class4 넘어가자ㅏㅏㅏ

This post is licensed under CC BY 4.0 by the author.