-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbfs.cpp
More file actions
67 lines (58 loc) · 1.29 KB
/
bfs.cpp
File metadata and controls
67 lines (58 loc) · 1.29 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
#include<iostream>
#include<queue>
#include<string>
#include<map>
#include<iterator>
using namespace std;
bool findElement(queue<string> q, string elem){
string temp;
while(!q.empty()){
temp = q.front();
q.pop();
if(temp == elem){return true;}
}
return false;
}
int getMin(string from, string to){
map< string, int > level,parent;
string temp,temp2;
parent[from] = 1;
level[from] = 0;
//cout<<level[from];
int l=1,length;
queue<string> frontier,next;
frontier.push(from);
cout<<findElement(frontier,from)<<endl;
//temp.push(to);
//frontier = temp;
//cout<<frontier.front()<<endl;
while(!frontier.empty()){
while(!next.empty())
next.pop();
for(int i=0; i<frontier.size(); ++i){
temp=frontier.front();
length = temp.length();
//cout<<temp.length()<<endl;
//cout<<length<<endl;
for(int x=0; x<length; ++x)
for(int y=x+1; y<length; ++y){
temp2 = temp;
swap(temp2[x],temp2[y]);
//cout<<temp2<<endl;
if(!findElement(frontier,temp2) && !(parent.count(temp2)>0)){
parent[temp2] = i;
level[temp2] = l;
next.push(temp2);
}
//if(temp2.compare(to) == 0){return level[temp2];}
}
frontier=next;
}
l++;
}
return level[to];
}
int main(){
string from="apple", to="elppa";
cout<< getMin(from, to) << endl;
}