728x90
using System;
using System.Collections.Generic;
public class Solution {
public bool canChange(string a, string b){
int same = 0;
for (int i =0; i < a.Length; i++){
if (a[i] == b[i]) same++;
}
if(same == a.Length-1) return true;
return false;
}
public int solution(string begin, string target, string[] words) {
Queue<(string, int)> queue = new Queue<(string, int)>();
queue.Enqueue((begin, 0));
bool[] visited = new bool[words.Length];
while(queue.Count > 0){
var (a,b) = queue.Dequeue();
if (a == target){
return b;
}
for(int i =0 ; i < words.Length; i++){
if (!visited[i] && canChange(a, words[i])){
visited[i] = true;
queue.Enqueue((words[i], b+1));
}
}
}
return 0;
}
}
사실 챗 지피티랑 이론적으로 머리 합친거를 혼자 코드로 옮겨 썼다...
728x90
'공부 > C#' 카테고리의 다른 글
[C#] 프로그래머스 백트래킹 - 피로도 (0) | 2025.03.07 |
---|---|
[C#] 프로그래머스 백트래킹 - 소수 찾기 (0) | 2025.03.07 |
[C#] 프로그래머스 DFS 연습 - 여행경로 (0) | 2025.03.07 |
[C#] 프로그래머스 DFS 연습 - 타겟 넘버 (0) | 2025.03.05 |
[C#] 프로그래머스 DFS 연습 - 네트워크 (0) | 2025.03.01 |