글
C# 단어 변환(DFS, 깊이 우선 탐색)
using System;
public class Solution {
string tar;
int answer = 0;
//dfs
//비교 항목, 단계, 방문 여부, 비교 대상 배열
public void dfs(string begin, int phase, bool[] visit, string[] words)
{
//비교 대상 길이만큼 반복
for(int i=0; i<words.Length; i++)
{
//해당 visit 변수가 거짓이면
if(!visit[i])
{
//비교 항목과 [i]번째 비교 대상 단어와 비교
int n = 0;
for(int j=0; j<words[i].Length; j++)
{
//한 글자씩 비교하여 같으면 n변수 더하기
if(begin[j] == words[i][j])
{
n++;
}
}
//변수 n이 비교 항목 문자열 길이 - 1 과 같을 때
if(n == begin.Length-1)
{
//비교 항목을 새로운 비교 대상 단어로 치환
begin = words[i];
//해당 번째 방문 true
visit[i] = true;
//치환된 비교 항목이 목표 타겟과 같으면 dfs 종료
if(begin == tar)
{
answer = phase + 1;
}
//아니면 dfs 실행
else
{
dfs(begin, phase + 1, visit, words);
}
}
}
}
}
public int solution(string begin, string target, string[] words) {
//목표 타겟 변수 저장
tar = target;
//방문 변수 선언
bool[] visit = new bool[words.Length];
//dfs 실행
dfs(begin, 0, visit, words);
return answer;
}
}
// 몰라서 베꼈다.
'프로그래밍' 카테고리의 다른 글
C# 가장 큰 수(foreach, CompareTo, var, string.Join) (0) | 2023.01.27 |
---|---|
C# 디스크 컨트롤러(다른 사람의 풀이) (0) | 2023.01.27 |
프로그래머스 레벨1 C# 체육복(list Remove) (0) | 2023.01.27 |
프로그래머스 C# K번째 수(list.Clear(), list.Count) (0) | 2023.01.27 |
프로그래머스 C# 큰 수 만들기(Stringbuilder) (0) | 2023.01.27 |