BOJ::12761 돌다리

https://www.acmicpc.net/problem/12761


8가지 경우에 대해 bfs로 탐색해 가장 먼저 도착하는게 몇번째인지 찾아주면 된다.


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
#include <stdio.h>
#include <queue>
using namespace std;
 
bool visited[100001];
int a, b, n, m, cur, time;
 
 
struct Node {
    int cur;
    int time;
    Node(int c, int t) {
        cur = c;
        time = t;
    }
};
 
void bfs(){
    queue<Node> q;
    q.push(Node(n, 0));
    visited[n] = true;
    int dx[] = { 1,-1,a,b,-a,-b,a,b };
    while (!q.empty()) {
        cur = q.front().cur;
        time = q.front().time;
        q.pop();
        if (cur == m) {
            printf("%d\n", time);
            return;
        }
        int next;
        for (int i = 0; i < 8; i++) {
            if (i >= 6) {
                next = cur * dx[i];
            }
            else {
                next = cur + dx[i];
            }
            if (next >= 0 && next <= 100000) {
                if (!visited[next]) {
                    q.push(Node(next, time + 1));
                    visited[next] = true;
                }
            }
        }
    }
}
 
int main() {
    scanf("%d %d %d %d"&a, &b, &n, &m);
    bfs();
}
cs


'BOJ::문제풀이' 카테고리의 다른 글

13458 시험 감독  (0) 2018.01.07
13305 주유소  (4) 2018.01.07
11727 2 x n 타일링2  (0) 2018.01.07
11581 구호물자  (0) 2018.01.07
11578 팀원 모집  (0) 2018.01.07

+ Recent posts