-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogrammers_b.cpp
More file actions
50 lines (42 loc) · 904 Bytes
/
programmers_b.cpp
File metadata and controls
50 lines (42 loc) · 904 Bytes
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int answer = 50;
int visited[50];
int check_diff(string a, string b){
int dif_cnt =0;
for(int i=0; i<a.size();i++){
if(a[i]!=b[i]){
dif_cnt++;
}
}
if(dif_cnt==1){
return 1;
}
return 0;
}
void dfs(string begin, string target, vector<string>words, int i){
if(answer <= i)
return;
if(begin==target){
answer =min(answer,i);
return;
}
for(int j=0; j<words.size();j++){
if(check_diff(begin,words[j])&& !visited[j]){
visited[j]=1;
dfs(words[j],target,words,i+1);
visited[j]=0;
}
}
}
int solution(string begin, string target, vector<string> words) {
dfs(begin,target,words,0);
if(answer == 50)
return 0;
return answer;
}
int main(){
}