티스토리 뷰

 

 

 

 

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
 
import java.util.Scanner;
 
public class HideSeek {
 
    static int min = Integer.MAX_VALUE;
    static int bro;
    static int su2;
    static boolean visited[] = new boolean[100001];
 
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
 
        su2 = sc.nextInt();
        bro = sc.nextInt();
 
        bfs(su2, bro);
    }
    //범위 체크
    public static boolean isRange(int x) {
        if (x >= 0 && x <= 100000)
            return true;
        return false;
    }
 
    public static void bfs(int su, int bro) {
        Queue<pair> queue = new LinkedList<>();
 
        visited[su] = true;
        queue.offer(new pair(su, 0));
 
        while (!queue.isEmpty()) {
            pair p = queue.poll();
            //위치
            int a = p.y;
            //시간
            int b = p.x;
 
            if (a == bro) {
                System.out.println(b);
                break;
            }
 
            if (isRange(a * 2&& !visited[a * 2]) {
                queue.offer(new pair(a * 2, b + 1));
                visited[a * 2= true;
            }
            if (isRange(a + 1&& !visited[a + 1]) {
                queue.offer(new pair(a + 1, b + 1));
                visited[a + 1= true;
            }
            if (isRange(a - 1&& !visited[a - 1]) {
                queue.offer(new pair(a - 1, b + 1));
                visited[a - 1= true;
            }
 
        }
 
    }
 
    static class pair {
        int y;
        int x;
 
        public pair(int y, int x) {
            this.y = y;
            this.x = x;
        }
 
    }
 
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
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
글 보관함