File tree Expand file tree Collapse file tree
weekly/week08/BOJ_13549_숨바꼭질3 Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package week08 .BOJ_13549_숨바꼭질3 ;
2+
3+ import java .util .*;
4+ import java .lang .*;
5+ import java .io .*;
6+
7+ class BOJ13549 {
8+ static boolean [] visit ;
9+ public static void main (String [] args ) throws IOException {
10+ BufferedReader br = new BufferedReader (new InputStreamReader (System .in ));
11+ StringTokenizer st = new StringTokenizer (br .readLine ());
12+ int N = Integer .parseInt (st .nextToken ()); //수빈
13+ int K = Integer .parseInt (st .nextToken ()); //동생
14+
15+ int answer = Integer .MAX_VALUE ;
16+ visit = new boolean [100001 ];
17+ Queue <Pos > queue = new LinkedList <>();
18+ queue .add (new Pos (N , 0 ));
19+ while (!queue .isEmpty ()) {
20+ Pos poll = queue .poll ();
21+ visit [poll .x ] = true ;
22+
23+ if (poll .x == K ) {
24+ answer = Math .min (answer , poll .time );
25+ }
26+
27+ if (check (poll .x + 1 )) queue .add (new Pos (poll .x + 1 , poll .time + 1 ));
28+ if (check (poll .x - 1 )) queue .add (new Pos (poll .x - 1 , poll .time + 1 ));
29+ if (check (poll .x * 2 )) queue .add (new Pos (poll .x * 2 , poll .time ));
30+ }
31+ System .out .println (answer );
32+ }
33+
34+ static boolean check (int value ) {
35+ return value >= 0 && value <= 100000 && !visit [value ];
36+ }
37+ }
38+
39+ class Pos {
40+ int x ;
41+ int time ;
42+
43+ Pos (int x , int time ) {
44+ this .x = x ;
45+ this .time = time ;
46+ }
47+ }
You can’t perform that action at this time.
0 commit comments