Skip to content

Commit cc32b67

Browse files
authored
Create 프로그래머스_숫자변환하기.java
1 parent c557a2c commit cc32b67

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import java.util.*;
2+
class Solution {
3+
public int solution(int x, int y, int n) {
4+
int answer = 0;
5+
return bfs(x,y,n);
6+
}
7+
8+
public int bfs(int x,int y, int n) {
9+
Queue<int[]> q = new LinkedList<>();
10+
boolean [] visit = new boolean[y + 1];
11+
q.offer(new int[] {x, 0});
12+
while(!q.isEmpty()) {
13+
int [] a = q.poll();
14+
if (a[0] == y) {
15+
return a[1];
16+
}
17+
for (int i = 0; i < 3;i++) {
18+
int na = 0;
19+
if (i == 0) {
20+
na = a[0] + n;
21+
} else if (i == 1) {
22+
na = a[0] * 2;
23+
} else if (i == 2) {
24+
na = a[0] * 3;
25+
}
26+
if (na <= y && !visit[na]) {
27+
q.offer(new int[] {na,a[1] + 1});
28+
visit[na] = true;
29+
}
30+
}
31+
}
32+
return -1;
33+
}
34+
}

0 commit comments

Comments
 (0)