티스토리 뷰
문제 출처 - https://programmers.co.kr/learn/courses/30/lessons/43163
코딩테스트 연습 - 단어 변환 | 프로그래머스
두 개의 단어 begin, target과 단어의 집합 words가 있습니다. 아래와 같은 규칙을 이용하여 begin에서 target으로 변환하는 가장 짧은 변환 과정을 찾으려고 합니다. 1. 한 번에 한 개의 알파벳만 바꿀 수 있습니다. 2. words에 있는 단어로만 변환할 수 있습니다. 예를 들어 begin이 hit, target가 cog, words가 [hot,dot,dog,lot,log,cog]라면 hit -> hot -> dot -> dog ->
programmers.co.kr
#include <string>
#include <vector>
using namespace std;
int answer = 100;
bool visit[50];
void dfs(int idx, string begin, string target, vector<string> words, int cnt) {
if(begin == target) {
if(answer > cnt)
answer = cnt;
return;
}
for(int i=0; i<words.size(); i++) {
int temp = 0; // 몇개의 문자가 다른지 카운트
// 방문하지 않았을때만
if(!visit[i]) {
for(int j=0; j<words[i].size(); j++) {
if(words[i][j] != begin[j])
temp++;
}
// 1개의 문자만 다른 경우 탐색
if(temp == 1) {
visit[i] = true;
dfs(i+1, words[i], target, words, cnt+1);
visit[i] = false;
}
}
}
}
int solution(string begin, string target, vector<string> words) {
bool flag = false;
// 타겟이 없으면 걍 리턴
for(int i=0; i<words.size(); i++) {
if(words[i] == target) {
flag = true;
break;
}
}
if(flag == false) answer = 0;
else dfs(0, begin, target, words, 0);
return answer;
}
temp를 같은 문자가 몇개인지 세는걸로 바꿔서 풀면 실패뜸.. 왜지...?
'ALGORITHM > 프로그래머스' 카테고리의 다른 글
[C++][완전탐색] 프로그래머스 - 소수 찾기(Level2) (2) | 2019.12.31 |
---|---|
[C++]프로그래머스 - 같은 숫자는 싫어(level1) (0) | 2019.12.27 |
[C++]프로그래머스 - 124 나라의 숫자(level2) (0) | 2019.12.06 |
[C++]프로그래머스 - 완주하지 못한 선수(level1) (0) | 2019.12.06 |
[C++]프로그래머스 - 올바른 괄호의 갯수(level4) (0) | 2019.11.30 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 재귀
- programmers
- 파이썬
- 스택
- combination
- Permutation
- Python
- 우선순위큐
- SW Expert
- 완전탐색
- SWExpert
- 백준
- 힙
- 구현
- 코딩테스트
- dictionary
- 해시
- 문자열
- 순열
- hash
- 정렬
- 2019 Kakao Blind Recruitment
- 괄호
- C++
- 문자열처리
- 2020 KAKAO BLIND RECRUITMENT
- 딕셔너리
- 프로그래머스
- left join
- BOJ
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함