프로그래머스(C++) - 단어 변환

2025. 9. 23. 21:02·C++ 프로그래머스 다이어리/BFS , DFS

●문제

●입출력

 

문제해석 : 현재 begin의 단어가 words 안에서 target단어로 가는데  한번에 한 단어씩만 바꿔서 

target으로 바뀌는 최소한의 변환절차를 카운팅 해서 return

bfs로 풀어도되고 dfs로 풀어도되는데 필자는 dfs로 풀었습니다

#include <vector>
#include <iostream>
#include <stdio.h>

using namespace std;
int answer = 0;
vector<bool> checked(51, 0); //방문여부
void DFS(int count, const string& current, const string& target, const vector<string>& words)
{
	//다른 글자수 찾기
	for (int i = 0; i < words.size(); ++i)
	{
		int diffCount = 0;

		for (int j = 0; j < words[i].size(); ++j)
		{
			//현재 글자가 words의 글자중에 다른게있다면 변수값 증가
			if (current[j] != words[i][j])
			{
				diffCount++;
			}
		}

		//다른 글자수가 한개고 방문하지않았을때
		if (diffCount == 1 && !checked[i])
		{
			//목표 단어를 찾은경우
			if (words[i] == target)
			{
				answer = count + 1;
				return;
			}
			else
			{
				checked[i] = true;
				DFS(count + 1, words[i], target, words);
			}
		}
		
	}
	
}
int solution(string begin, string target, vector<string> words)
{
	DFS(0, begin, target, words);
	return answer;
}

 

레벨 : 3

점수 : 1

'C++ 프로그래머스 다이어리 > BFS , DFS' 카테고리의 다른 글

프로그래머스(C++) - 여행경로  (0) 2025.10.05
프로그래머스(C++) - 게임 맵 최단거리  (0) 2025.09.14
프로그래머스(C++) - 네트워크  (0) 2025.09.13
프로그래머스(C++) - 타겟 넘버  (0) 2025.09.13
Algorithm - BFS, DFS  (0) 2025.09.12
'C++ 프로그래머스 다이어리/BFS , DFS' 카테고리의 다른 글
  • 프로그래머스(C++) - 여행경로
  • 프로그래머스(C++) - 게임 맵 최단거리
  • 프로그래머스(C++) - 네트워크
  • 프로그래머스(C++) - 타겟 넘버
lucodev
lucodev
커피와 노트북 그리고 개발
  • lucodev
    루코 개발테이블
    lucodev
  • 전체
    오늘
    어제
    • 분류 전체보기 (171) N
      • Unreal5 프로젝트 다이어리 (73)
      • Unreal5 프로젝트 다이어리2 (11)
      • Unreal 팁 (8)
      • Unreal 디버깅 (8)
      • 코드 개인보관함 (8)
      • C++ 프로그래머스 다이어리 (49) N
        • Stack,Queue (6)
        • Hash (4)
        • Heap (2)
        • Sort (5)
        • Exhaustive search (5)
        • Greedy (2)
        • BFS , DFS (6)
        • Graph (2)
        • Dynamic Programming (1)
        • C++ Math (2)
        • 기타 문제 (13) N
      • 코딩테스트+@ (10) N
      • 알고리즘 스타디 (1)
      • 알고리즘 스타디 과제 (3)
  • 인기 글

  • 최근 글

  • 최근 댓글

  • 링크

  • 공지사항

  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 태그

    unreal look at
    언리얼 foot step
    언리얼 behaviortree
    unreal 시퀀스
    언리얼 behavior tree
    언리얼 컷씬
    언리얼 페이드 아웃
    unreal 로딩
    unreal sequence
    unreal 모션매칭
    언리얼 모션매칭
    언리얼 로딩창
    unreal loading
    언리얼 motionmatching
    언리얼 시퀀스
    언리얼 비헤이비어트리
    unreal 컷씬
    언리얼
    언리얼 로딩
    언리얼 look at
  • hELLO· Designed By정상우.v4.10.3
lucodev
프로그래머스(C++) - 단어 변환
상단으로

티스토리툴바