-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWord_Ladder.h
55 lines (52 loc) · 1.32 KB
/
Word_Ladder.h
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
51
52
53
54
55
//
// Word_Ladder.h
// algorithm
//
// Created by DanMiao on 9/9/16.
// Copyright © 2016 DanMiao. All rights reserved.
//
#ifndef Word_Ladder_h
#define Word_Ladder_h
int countdiff(string i, string j)
{
int count = 0;
for(int k = 0; k < i.size(); k++)
{
if(i[k] != j[k])
count++;
}
return count;
}
void dfs(string beginWord, string endWord, unordered_set<string>& wordList, int& result, int count, vector<bool>& record)
{
if(beginWord == endWord)
{
if(count < result)
result = count+1;
return;
}
if(wordList.size() == 0)
return;
string buf = beginWord;
int i = 0;
for(auto it = wordList.begin(); it != wordList.end(); it++, i++)
{
if(countdiff(beginWord, *it) == 1 && record[i])
{
beginWord = *it;
count++;
record[i] = false;
dfs(beginWord, endWord, wordList, result, count, record);
count--;
record[i] = true;
beginWord = buf;
}
}
}
int ladderLength(string beginWord, string endWord, unordered_set<string>& wordList) {
int result = INT_MAX;
vector<bool> record(wordList.size(), true);
dfs(beginWord, endWord, wordList, result, 0, record);
return result == INT_MAX ? 0 : result;
}
#endif /* Word_Ladder_h */