From 115cac1d69818021cbc4e3392b4700db24db8a7a Mon Sep 17 00:00:00 2001 From: lkhyun <102892446+lkhyun@users.noreply.github.com> Date: Fri, 14 Nov 2025 23:38:58 +0900 Subject: [PATCH] =?UTF-8?q?[20251114]=20PGM=20/=20Lv3=20/=20=EB=8B=A8?= =?UTF-8?q?=EC=96=B4=20=EB=B3=80=ED=99=98=20/=20=EC=9D=B4=EA=B0=95?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0\354\226\264 \353\263\200\355\231\230.md" | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 "lkhyun/202511/14 PGM Lv3 \353\213\250\354\226\264 \353\263\200\355\231\230.md" diff --git "a/lkhyun/202511/14 PGM Lv3 \353\213\250\354\226\264 \353\263\200\355\231\230.md" "b/lkhyun/202511/14 PGM Lv3 \353\213\250\354\226\264 \353\263\200\355\231\230.md" new file mode 100644 index 00000000..40746301 --- /dev/null +++ "b/lkhyun/202511/14 PGM Lv3 \353\213\250\354\226\264 \353\263\200\355\231\230.md" @@ -0,0 +1,51 @@ +```java +class Solution { + static int answer; + static boolean[] visited; + + public int solution(String begin, String target, String[] words) { + boolean hasTarget = false; + for(String word : words) { + if(word.equals(target)) { + hasTarget = true; + break; + } + } + if(!hasTarget) return 0; + + answer = Integer.MAX_VALUE; + visited = new boolean[words.length]; + DFS(begin, target, words, 0); + + return answer == Integer.MAX_VALUE ? 0 : answer; + } + + public static void DFS(String from, String target, String[] words, int depth){ + if(target.equals(from)){ + answer = Math.min(answer, depth); + return; + } + + for(int i = 0; i < words.length; i++){ + if(visited[i]) continue; + + if(isOneDifferent(from, words[i])){ + visited[i] = true; + DFS(words[i], target, words, depth + 1); + visited[i] = false; + } + } + } + + private static boolean isOneDifferent(String word1, String word2) { + int diff = 0; + for(int i = 0; i < word1.length(); i++){ + if(word1.charAt(i) != word2.charAt(i)){ + diff++; + if(diff > 1) return false; + } + } + return diff == 1; + } +} +```