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 |
Tags
- 백준 17822
- 백준 5397
- 백준 17779
- 원판 돌리기
- ㅣ풀이
- 풀이
- 구현
- 스택의 특징
- 자료구조
- 백준 17471
- 시간 복잡도
- heap
- 백준 1158
- 별 찍기 10
- AVL 시간 복잡도
- 5397
- 1764
- Stack 이란
- 버킷 정렬
- 백준 1406
- 게리멘더링2
- 백준 2447
- c#
- dfs
- 조세퍼스 순열
- 해시구현
- 해시 구현
- C/C++ 구현
- 백준
- qorwns
Archives
- Today
- Total
홍시홍의 프로그래밍
[프로그래머스] 단어 변화 본문
분류 : dfs/bfs
요구사항
최저의 이동으로 str을 target으로 변환시키기
풀이
최저 이동으로 target으로 변환시켜야 된다
조건은 now->next로 이동할때 단어가 1개 달라야지 이동할 수 있다는 것이다.
이 조건만 처리해주면 일반적인 dfs로 풀이하면 된다
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
vector<string> v;
string m;
int ans = 987654321;
int visit[100];
int check(string a, string b) {
int j = 0;
int i = 0;
int cnt = 0;
while (a[i]) {
if (a[i++] != b[j++]) {
cnt++;
}
if (cnt == 2) {
break;
}
}
return cnt;
}
void dfs(string str, int num) {
if (str == m) {
ans = min(ans, num);
}
for (int i = 0; i < v.size(); i++) {
if (visit[i] == 0) {
if (check(str, v[i]) == 1) {
visit[i] = 1;
dfs(v[i], num + 1);
visit[i] = 0;
}
}
}
}
int solution(string begin, string target, vector<string> words) {
int answer = 0;
m = target;
v = words;
cout << m << endl;
dfs(begin, 0);
if (ans == 987654321)
ans = 0;
else
answer = ans;
return answer;
}
'알고리즘 문제풀이 > 프로그래머스' 카테고리의 다른 글
[프로그래머스] 가장 큰 수 (0) | 2020.04.18 |
---|---|
[프로그래머스] k번째 수 (0) | 2020.04.18 |
[프로그래머스] 네트워크 (0) | 2020.04.10 |
[프로그래머스] 타겟 넘버 (0) | 2020.04.10 |
[프로그래머스] 베스트앨범 (0) | 2020.04.10 |
Comments