https://leetcode.com/problems/word-ladder/
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest transformation sequence from beginWord to endWord, such that:
- Only one letter can be changed at a time.
- Each transformed word must exist in the word list. Note that beginWord is not a transformed word.
Note:
- Return 0 if there is no such transformation sequence.
- All words have the same length.
- All words contain only lowercase alphabetic characters.
- You may assume no duplicates in the word list.
- You may assume beginWord and endWord are non-empty and are not the same.
Example 1:
Input:
beginWord = "hit",
endWord = "cog",
wordList = ["hot","dot","dog","lot","log","cog"]
Output: 5
Explanation: As one shortest transformation is "hit" -> "hot" -> "dot" -> "dog" -> "cog",
return its length 5.
Example 2:
Input:
beginWord = "hit"
endWord = "cog"
wordList = ["hot","dot","dog","lot","log"]
Output: 0
Explanation: The endWord "cog" is not in wordList, therefore no possible transformation.
功力退化啊 ……
区区搜索dfs返回值有问题,看到后面提示说是有bfs,吭哧吭哧写那么多
point:
1.step是随着状态变而不是随着while变,这个是记录在每一步的结构体信息中的
2.不一定一定要优先队列,普通的一样可以
3.真的和普通的bfs没多大区别,咋写的这么费劲
class Solution {
public:
struct node {
int pos,step;
node(int apos, int astep) {
pos = apos;
step = astep;
}
};
bool comp(string x, string y) {
if (x.length() != y.length())
return false;
int cnt = 0;
for (int i = 0; i < x.length(); i ++) {
if (x[i] == y[i])
cnt ++;
}
if (cnt == x.length()-1)
return true;
return false;
}
int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
int len = wordList.size();
vector<bool> vis(len, false);
queue<node> q;
for (int i = 0; i < wordList.size(); i ++) {
if (comp(beginWord, wordList[i])) {
q.push(node(i,1));
vis[i] = true;
//break;
}
}
while(!q.empty()) {
node x = q.front();
cout<< wordList[x.pos] << x.step << endl;
if (wordList[x.pos] == endWord)
return x.step+1;
q.pop();
for (int i = 0; i < wordList.size(); i ++) {
if (vis[i] == false && comp(wordList[x.pos], wordList[i])) {
q.push(node(i, x.step + 1));
vis[i] = true;
}
}
}
return 0;
}
};
class Solution {
public:
struct node {
int pos,step;
node(int apos, int astep) {
pos = apos;
step = astep;
}
friend bool operator <(node a,node b)
{
return b.step<a.step;///
}
};
bool comp(string x, string y) {
if (x.length() != y.length())
return false;
int cnt = 0;
for (int i = 0; i < x.length(); i ++) {
if (x[i] == y[i])
cnt ++;
}
if (cnt == x.length()-1)
return true;
return false;
}
int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
int len = wordList.size();
vector<bool> vis(len, false);
priority_queue<node> q;
for (int i = 0; i < wordList.size(); i ++) {
if (comp(beginWord, wordList[i])) {
q.push(node(i,1));
vis[i] = true;
//break;
}
}
while(!q.empty()) {
node x = q.top();
cout<< wordList[x.pos] << x.step << endl;
if (wordList[x.pos] == endWord)
return x.step+1;
q.pop();
for (int i = 0; i < wordList.size(); i ++) {
if (vis[i] == false && comp(wordList[x.pos], wordList[i])) {
q.push(node(i, x.step + 1));
vis[i] = true;
}
}
}
return 0;
}
};
试着改了一个deque的
class Solution {
public:
struct node {
int pos,step;
node(int apos, int astep) {
pos = apos;
step = astep;
}
};
bool comp(string x, string y) {
if (x.length() != y.length())
return false;
int cnt = 0;
for (int i = 0; i < x.length(); i ++) {
if (x[i] == y[i])
cnt ++;
}
if (cnt == x.length()-1)
return true;
return false;
}
int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
int len = wordList.size();
vector<bool> vis(len, false);
deque<node> q;
for (int i = 0; i < wordList.size(); i ++) {
if (comp(beginWord, wordList[i])) {
q.push_front(node(i,1));
vis[i] = true;
//break;
}
}
while(!q.empty()) {
node x = q.front();
//cout<< wordList[x.pos] << x.step << endl;
if (wordList[x.pos] == endWord)
return x.step+1;
q.pop_front();
for (int i = 0; i < wordList.size(); i ++) {
if (vis[i] == false && comp(wordList[x.pos], wordList[i])) {
q.push_back(node(i, x.step + 1));
vis[i] = true;
}
}
}
return 0;
}
};
下面是lintcode的版本 unorder_set真是烦死了
class Solution {
public:
/*
* @param start: a string
* @param end: a string
* @param dict: a set of string
* @return: An integer
*/
struct node {
string pos;
int step;
node(string apos, int astep) {
pos = apos;
step = astep;
}
};
bool comp(string x, string y) {
if (x.length() != y.length())
return false;
int cnt = 0;
for (int i = 0; i < x.length(); i ++) {
if (x[i] == y[i])
cnt ++;
}
if (cnt == x.length()-1)
return true;
return false;
}
int ladderLength(string &start, string &end, unordered_set<string> &dict) {
int len = dict.size();
vector<bool> vis(len, false);
deque<node> q;
int i = 0;
if (comp(start, end)) {
return 2;
}
for (unordered_set<string>::iterator word = dict.begin(); word != dict.end(); word++) {
if (comp(start, *word)) {
cout<<"word:"<<*word <<" step:1" <<endl;
q.push_front(node(*word,2));
vis[i] = true;
//break;
}
i ++;
}
while(!q.empty()) {
node x = q.front();
//cout<< wordList[x.pos] << x.step << endl;
if (comp(x.pos,end)) {
cout<<x.pos<<endl;
return x.step+1;
}
q.pop_front();
int i = 0;
for (unordered_set<string>::iterator word = dict.begin(); word != dict.end(); word++) {
if (vis[i] == false && comp(x.pos, *word)) {
cout<<"word:"<<*word <<" step:" <<x.step+1<<endl;
q.push_back(node(*word, x.step + 1));
vis[i] = true;
}
i++;
}
}
return 0;
}
};
其实queue就可以的。。。。
class Solution {
public:
struct node {
int pos,step;
node(int apos, int astep) {
pos = apos;
step = astep;
}
};
bool comp(string x, string y) {
if (x.length() != y.length())
return false;
int cnt = 0;
for (int i = 0; i < x.length(); i ++) {
if (x[i] == y[i])
cnt ++;
}
if (cnt == x.length()-1)
return true;
return false;
}
int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
int len = wordList.size();
vector<bool> vis(len, false);
queue<node> q;
for (int i = 0; i < wordList.size(); i ++) {
if (comp(beginWord, wordList[i])) {
q.push(node(i,1));
vis[i] = true;
//break;
}
}
while(!q.empty()) {
node x = q.front();
//cout<< wordList[x.pos] << x.step << endl;
if (wordList[x.pos] == endWord)
return x.step+1;
q.pop();
for (int i = 0; i < wordList.size(); i ++) {
if (vis[i] == false && comp(wordList[x.pos], wordList[i])) {
q.push(node(i, x.step + 1));
vis[i] = true;
}
}
}
return 0;
}
};