250x250
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- joystick
- 오브젝트 풀링
- rotation
- raycast
- CSharp #자료구조
- Animation State Machine
- 플레이어 이동
- Hp바
- Unity
- Ainimation Blending
- Transform
- Parallax
- raycasting
- 유니티
- Object Pooling
- 패럴렉스
- 2D슈팅게임
- rigidbody
- Scrooling
- apk
- 플레이어 방향전환
- Blend Type
- 스크롤링
- Prefabs
- 일시정지
- 프리팹
- Hpbar
- Object Poling
- Vector3
- LayerMark
Archives
- Today
- Total
주녘공부일지
[프로그래머스 C#] Lv.3 단어 변환 본문
728x90
https://school.programmers.co.kr/learn/courses/30/lessons/43163
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
1. 정답코드 및 핵심 아이디어, 유의사항
시작 단어에서 목표 단어로 변환하기까지의 최소 변환 횟수를 구하는 문제
- 주어진 단어들로만 변환할 수 있음
- 단어 내의 알파벳 중 단 하나만 다른 경우에만 변환할 수 있음
- 만약, 변환할 수 없는 경우 0을 리턴
최소 변환 횟수를 구하는 문제 : BFS 알고리즘 (너비 우선 탐색) 적용
+ 한번 변환했던 단어로는 다시 변환해도 의미가 없음
https://godgjwnsgur7.tistory.com/47
[Algorithm C#] BFS, DFS ( + Back Tracking )
1. BFS(Breadth First Search) - 너비 우선 탐색 최단 경로, 임의의 경로를 찾고 싶을 때, 등에 사용 - 큐(FIFO) 사용 // 큐를 이용 public void BFS(int index) { Node root = nodes[index]; Queue queue = new Queue(); queue.Enqueue(root
godgjwnsgur7.tistory.com
주석 참조
using System;
using System.Collections.Generic;
public class Solution
{
public class DataClass
{
public string word; // 현재단어
public int count; // 누적변환횟수
public DataClass(string word, int count)
{
this.word = word;
this.count = count;
}
}
public int solution(string begin, string target, string[] words)
{
var boolArray = new bool[words.Length]; // 방문배열
var queue = new Queue<DataClass>(); // BFS Queue
queue.Enqueue(new DataClass(begin, 0)); // 초기 값 세팅
// BFS 탐색
while (queue.Count > 0)
{
DataClass data = queue.Dequeue();
for (int i = 0; i < words.Length; i++)
{
// 이미 변환했던 단어거나 변환할 수 없는 경우
if (boolArray[i] || Function(words[i], data.word) == false)
continue;
// target으로 변환이 가능한 경우
if (target == words[i])
return data.count + 1;
// 변환이 가능한 단어를 찾은 경우
boolArray[i] = true;
queue.Enqueue(new DataClass(words[i], data.count + 1));
}
}
return 0;
}
// 변환이 가능한 두 단어인지 판단하는 메서드
public bool Function(string s1, string s2)
{
int count = 0;
for (int i = 0; i < s1.Length; i++)
if (s1[i] != s2[i])
count++;
return count == 1; // 변환 가능 조건
}
}
728x90
'Programmers - C# > CodingTest Lv.3' 카테고리의 다른 글
[프로그래머스 C#] Lv.3 여행경로 (0) | 2024.03.21 |
---|---|
[프로그래머스 C#] Lv.3 가장 먼 노드 (0) | 2024.03.20 |
[프로그래머스 C#] Lv.3 네트워크 (0) | 2024.03.14 |
[프로그래머스 C#] Lv.3 베스트앨범 (0) | 2024.03.13 |
[프로그래머스 C#] Lv.3 N으로 표현 (0) | 2024.03.11 |