๋ฌธ์
์ฝ๋
#include <string>
#include <vector>
using namespace std;
bool visited[1001];
int cnt,ans=9999;
void dfs(string begin,string target,int cnt, vector<string>& words){
if(begin==target) ans=min(ans,cnt);
for(int i=0; i<words.size(); i++){
int check=0;
for(int k=0; k<words[i].length(); k++){
if(words[i][k]!=begin[k]) check++;
}
if(check==1){
if(visited[i]) continue; //์ด๋ฏธ ํ์ํ๋ค๋ฉด ํ์์ํจ.
visited[i]=true;
dfs(words[i],target,cnt+1, words);
cnt--;
}
}
}
int solution(string begin, string target, vector<string> words) {
int answer = 0;
dfs(begin,target,cnt,words);
if(ans!=9999) answer=ans;
return answer;
}
#include <string>
#include <vector>
#include <algorithm>
#include <unordered_map>
using namespace std;
int answer=1e9;
unordered_map<string, int>visited;
bool check(string s1, string s2){
int cnt = 0;
for(int i=0; i<s1.length(); i++){
if(s1[i]!=s2[i]) cnt++;
}
if(cnt == 1) return true;
return false;
}
void dfs(string now, string target, vector<string>words, int depth){
if(now == target){
answer = min(answer, depth);
return;
}
for(int i=0; i<words.size(); i++){
if(visited[words[i]]) continue;
if(check(now, words[i])){
visited[words[i]] = 1;
dfs(words[i], target, words, depth+1);
visited[words[i]] = 0;
}
}
}
int solution(string begin, string target, vector<string> words) {
unordered_map<string, int> m;
for(string s: words) m[s]++;
if(!m[target]) return 0;
dfs(begin, target, words, 0);
return answer;
}
'Algorithm ๐ง๐ปโ๐ป > ํ๋ก๊ทธ๋๋จธ์ค(Programmers)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[c++] ํ๋ก๊ทธ๋๋จธ์ค - ๋ ๊ฐ ๋ฝ์์ ๋ํ๊ธฐ( Level 1) (0) | 2021.10.20 |
---|---|
[c++] ํ๋ก๊ทธ๋๋จธ์ค - ๋ ๋งต๊ฒ( Level 2) (0) | 2021.10.20 |
[c++] ํ๋ก๊ทธ๋๋จธ์ค - ๋จ์ฒด์ฌ์ง ์ฐ๊ธฐ( Level 2, 2017 ์นด์นด์ค์ฝ๋ ๋ณธ์ ) (0) | 2021.10.20 |
[c++] ํ๋ก๊ทธ๋๋จธ์ค - ๋คํธ์ํฌ( Level 3) (0) | 2021.10.20 |
[c++] ํ๋ก๊ทธ๋๋จธ์ค - ๊ดํธ ๋ณํ( Level 2, 2020 KAKAO BLIND) (0) | 2021.10.20 |
[c++] ํ๋ก๊ทธ๋๋จธ์ค - ๊ฒ์ ๋งต ์ต๋จ๊ฑฐ๋ฆฌ (Level 2) (0) | 2021.10.19 |
๋๊ธ