-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathJY_43163.java
More file actions
55 lines (49 loc) · 1.52 KB
/
JY_43163.java
File metadata and controls
55 lines (49 loc) · 1.52 KB
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
import java.util.*;
class JY_43163 {
static class State{
String word;
int cnt;
public State(String word, int cnt){
this.word = word;
this.cnt = cnt;
}
}
public int solution(String begin, String target, String[] words) {
int answer = 0;
// words에 target이 없으면 0
if(!isInclude(target, words)) return answer;
// BFS로 words에 있는 단어중 target 찾기
int N = words.length;
boolean[] visited = new boolean[N];
Queue<State> q = new LinkedList<>();
q.add(new State(begin, 0));
while(!q.isEmpty()){
State now = q.poll();
if(now.word.equals(target)){
answer = now.cnt;
break;
}
for(int i=0; i<N; i++){
if(!visited[i]){
int wc = 0;
for(int j=0; j<words[i].length(); j++){
if(words[i].charAt(j) == now.word.charAt(j)) wc++;
}
if(now.word.length() - wc == 1){
visited[i] = true;
q.add(new State(words[i], now.cnt+1));
}
}
}
}
return answer;
}
public static boolean isInclude(String target, String[] words){
for(String word: words){
if(target.equals(word)){
return true;
}
}
return false;
}
}