class Solution {
public:
struct info
{
int id;
int step;
};
void gen(vector
{
vector
vector
if(c[id].size()==0)
{
v.push_back(vstr[id]);
res.push_back(v);
return;
}
int i,j;
for(i=0;i
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector
vector
if(start==end) //start is end
{
v.push_back(start);
res.push_back(v);
return res;
}
unordered_map
unordered_map
unordered_set
vector
dict.insert(start);
dict.insert(end);
int id=0,i,j,k;
for(iter_s=dict.begin();iter_s!=dict.end();++iter_s)
{
vstr.push_back(*iter_s);
mp.insert(make_pair(*iter_s,id));
id++;
}
int nStart, nEnd;
iter_m = mp.find(start);
nStart = iter_m->second;
iter_m = mp.find(end);
nEnd = iter_m->second;
int len = start.length();
int nTotal = mp.size();
vector
int *step = new int [nTotal];
for(i=0;i
q.push(temp);
while(!q.empty())
{
cur=q.front();
q.pop();
if(cur.step>=nMin) continue;
string str = vstr[cur.id];
for(i=0;i
if(step[id]<=cur.step) continue;
if(step[id]!=cur.step+1)
{
temp.id=id;
temp.step=cur.step+1;
q.push(temp);
step[id]=cur.step+1;
if(id==nEnd) nMin = cur.step+1; //find end
}
c[id].push_back(cur.id);
}
}
}
if(c[nEnd].size()==0) //no result
{
delete[]c;
delete[]step;
return res;
}
gen(vstr,c,nEnd,res);
delete[]c;
delete[]step;
return res;
}
};
这不是Word Ladder 2嘛!