YoonWould!!

[BFS]1697번 숨바꼭질 본문

<SW>/알고리즘 + 자료구조

[BFS]1697번 숨바꼭질

Hading 2018. 8. 13. 00:00
728x90

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


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<queue>
using namespace std;
 
int n, k;
int visited[100001= { 0, };
 
int bfs(int start) {
    queue<int> q;
    q.push(start);
    visited[start] = 1;
    while (!q.empty()) {
        int y = q.front();
        q.pop();
        if (y == k) {
            return visited[y] - 1;
        }
        if (y - 1 <= 100000 && visited[y - 1== 0) {
            visited[y - 1= visited[y] + 1;
            q.push(y - 1);
        }
        if (y + 1 <= 100000 && visited[y + 1== 0) {
            visited[y + 1= visited[y] + 1;
            q.push(y + 1);
        }
        if (y *2 <= 100000 && visited[y*2== 0) {
            visited[y *2= visited[y] + 1;
            q.push(y *2);
        }
    }
}
 
int main() {
    scanf_s("%d %d"&n, &k);
 
    printf("%d",bfs(n));
    return 0;
}
cs


728x90

'<SW> > 알고리즘 + 자료구조' 카테고리의 다른 글

[삼성SWTest준비]14502번 연구소  (0) 2018.09.22
[BFS] 7576번 토마토  (0) 2018.08.14
[BFS]2178번 미로탐색  (0) 2018.08.11
1260번 DFS와 BFS  (0) 2018.03.28
Sequential Search 구현  (0) 2018.03.28