:: ADVANCE ::

[dovelet][BFS] 도망간 소를 잡아라 / catch_cow 본문

Algorithm/ES (완전탐색)

[dovelet][BFS] 도망간 소를 잡아라 / catch_cow

KSJ14 2015. 1. 16. 02:27
반응형

dovelet    14 단계 BFS


도망간 소를 잡아라 / catch_cow


http://59.23.113.171/30stair/catch_cow/catch_cow.php?pname=catch_cow


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
#include <stdio.h>
 
int queue[1000000][2];
int visit[1000010];
 
int main(void)
{
    int start, end;
    int N, K;
 
    scanf("%d%d", &N, &K);
 
    if (N == K)    {
        printf("0\n");
        return 0;
    }
    else if (N > K)    {
        printf("%d\n", N - K);
        return 0;
    }
  
    visit[queue[end][0]] = 1;
    queue[end][0] = N;
    queue[end++][1] = 0;
 
    while (start <= end)    {
 
        if (queue[start][0] == K)    {
            printf("%d\n", queue[start][1]);
            return 0;
        }
 
        if (queue[start][0] < K && visit[queue[start][0] * 2] == 0)    {
            visit[queue[start][0] * 2] = 1;
            queue[end][0] = queue[start][0] * 2;
            queue[end++][1] = queue[start][1] + 1;
        }
        if (queue[start][0] + 1 <= K && visit[queue[start][0] + 1] == 0)    {
            visit[queue[start][0] + 1] = 1;
            queue[end][0] = queue[start][0] + 1;
            queue[end++][1] = queue[start][1] + 1;
        }
        if (visit[queue[start][0] - 1] == 0)    {
            queue[end][0] = queue[start][0] - 1;
            queue[end++][1] = queue[start][1] + 1;
        }
 
        start++;
    }
 
    return 0;
}
cs


반응형
Comments