-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp43163.java
50 lines (44 loc) · 1.47 KB
/
p43163.java
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
package programmers;
public class P43163 {
int answer = 0; // 몇번만에 찾았는지?
public int solution(String begin, String target, String[] words) {
boolean[] visited = new boolean[words.length];
for(int i=0; i<visited.length; i++) {
visited[i] = false;
}
for(int i=0; i<words.length; i++) {
if (target.equals(words[i])) {
dfs(visited, words, begin, target, 0);
return answer;
}
}
return 0; // 아예 없을 때
}
public void dfs(boolean[] visited, String[] words, String begin, String target, int count) {
if(begin.equals(target)) {
answer = count;
return;
}
for(int i=0; i<words.length; i++) {
if(visited[i] == false && countSameWord(begin, words[i])) {
// System.out.println(words[i] + " " + count);
visited[i] = true;
dfs(visited, words, words[i], target, count+1);
visited[i] = false;
}
}
}
// 한글자만 빼고 다 똑같은 문자인지 확인
public boolean countSameWord(String word, String begin) {
int count = 0;
for (int i = 0; i < word.length(); i++) {
if (word.charAt(i) == begin.charAt(i)) {
count++;
}
}
if (count == word.length() - 1) {
return true;
}
return false;
}
}