[프로그래머스 / C++] 단어 변환

Seulguo·2022년 7월 10일
0

Algorithm

목록 보기
30/185
post-thumbnail
post-custom-banner

🐣 문제

링크 : https://school.programmers.co.kr/learn/courses/30/lessons/43163


🐥 코드

#include <string>
#include <vector>
#include <iostream>
using namespace std;

string target_word;
vector<string> words_list;
bool check[51] = {false};
int answer = 100;

void dfs(string begin, int result) {
    
	if (target_word == begin) {
		answer = result;
		return;
	}
    
	for (int i = 0; i < words_list.size(); i++) {
        
		int count = 1;
        
		for (int j = 0; j < words_list[i].size(); j++) {
			if (begin[j] != words_list[i][j]) count++;
			if (count == 3) break;
		}
		
		if (count == 2) {
			if (check[i] == false) {
				check[i] = true;
				dfs(words_list[i], result + 1);
				check[i] = false;
			}
		}
        
	}
}


int solution(string begin, string target, vector<string> words) {
	target_word = target;
	words_list = words;
	dfs(begin,0);
	if (answer == 100) answer = 0;
	return answer;
}
post-custom-banner

0개의 댓글