大佬教程收集整理的这篇文章主要介绍了c – 寻找BFS的方式,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
add("berlin",london") add("budapest","london") add("london","glassgow") add("budapest","moscow") find("berlin",moscow") // should return berlin,london,budapest,moscow
我已经定义了一个队列
struct node { String info; node *next; }; /*----------------------------------------------------*/ class Queue { private: node *front; node *rear; public: Queue(); ~Queue(); bool isEmpty(); void enqueue(String ); String dequeue(); void display(); bool find(String); }; /*----------------------------------------------------*/ void Queue::display(){ node *p = new node; p = front; if(front == NULL){ cout<<"Nothing to Display" << endl; }else{ while(p!=NULL){ cout<<p->info << endl; p = p->next; } } } /*----------------------------------------------------*/ Queue::Queue() { front = NULL; rear = NULL; } /*----------------------------------------------------*/ Queue::~Queue() { @R_674_9421@e front; } /*----------------------------------------------------*/ void Queue::enqueue(String data) { node *temp = new node(); temp->info = data; temp->next = NULL; if(front == NULL){ front = temp; }else{ rear->next = temp; } rear = temp; } /*----------------------------------------------------*/ String Queue::dequeue() { node *temp = new node(); String value; if(front == NULL){ cout<<"Queue is Emtpty" << endl; }else{ temp = front; value = temp->info; front = front->next; @R_674_9421@e temp; } return value; } /*----------------------------------------------------*/ bool Queue::isEmpty() { return (front == @R_944_6633@ } bool Queue::find( String Name){ node *temp = rear; while( temp != nullptr ){ if( temp -> info == Name){ return true; } temp = temp -> next; } return false; }
并试图实现bfs
class Graph { private: map< String,map<String,int >> graf; public: Graph(){}; ~Graph(){}; bool isConnected(String,String ); void addEdge (String one,String two); void BFS(String,String); }; /*----------------------------------------------------*/ bool Graph::isConnected(String one,String two){ return (graf[one][two] == 1 || graf[two][one] == 1 ); } /*----------------------------------------------------*/ void Graph::addEdge( String one,String two){ graf [one][two] = graf[two][one] = 1; } /*----------------------------------------------------*/ void Graph::BFS(String s,String m){ Queue one; map<String,bool> check; vector<String> path; for( auto &x : graf){ check[x.first] = false; } check[s] = true; one.enqueue(s); path.push_BACk(s); String tmp = one.dequeue(); for( auto &x: graf){ if( isConnected(tmp,x.first) && !check[x.first] ){ one.enqueue(x.first); check[x.first] = true; path.push_BACk(x.first); if(x.first == m){ break; } } } for( auto &x: path ) cout << x << " "; }
找到正确的“城镇”并存储它们进行印刷.问题是,这确实打印了所有可能性而不仅仅是正确的方法.例如,如果前面提到它也找到“布达佩斯 – 伦敦”并打印出来.我知道问题在于我在路上排列了每个“城镇”但未找到如何检查其正确性的方法.
我不确定我怎么能找到唯一(最高的)方式.我最近发现了这个alghoritm并且无法让他工作.我怎样才能改善我实施的这种方式以这种方式行事?
如果未设置父级,则不检查,因此if语句还检查父级是否已设置.
最后,您只需要通过父母,直到您到达目的地.
还请注意,我将BFS更改为从目的地开始.我这样做是因为否则从最后一个节点向第一个节点迭代将返回你需要的路径的反向.
这是代码:
#include <iostream> #include <map> using namespace std; struct node { String info; node *next; }; /*----------------------------------------------------*/ class Queue { private: node *front; node *rear; public: Queue(); ~Queue(); bool isEmpty(); void enqueue(String ); String dequeue(); void display(); bool find(String); }; /*----------------------------------------------------*/ void Queue::display(){ node *p = new node; p = front; if(front == NULL){ cout<<"Nothing to Display" << endl; }else{ while(p!=NULL){ cout<<p->info << endl; p = p->next; } } } /*----------------------------------------------------*/ Queue::Queue() { front = NULL; rear = NULL; } /*----------------------------------------------------*/ Queue::~Queue() { @R_674_9421@e front; } /*----------------------------------------------------*/ void Queue::enqueue(String data) { node *temp = new node(); temp->info = data; temp->next = NULL; if(front == NULL){ front = temp; }else{ rear->next = temp; } rear = temp; } /*----------------------------------------------------*/ String Queue::dequeue() { node *temp = new node(); String value; if(front == NULL){ cout<<"Queue is Emtpty" << endl; }else{ temp = front; value = temp->info; front = front->next; @R_674_9421@e temp; } return value; } /*----------------------------------------------------*/ bool Queue::isEmpty() { return (front == @R_944_6633@ } bool Queue::find( String Name){ node *temp = rear; while( temp != nullptr ){ if( temp -> info == Name){ return true; } temp = temp -> next; } return false; } class Graph { private: map< String,String> check; for( auto &x : graf){ check[x.first] = "-"; } check[m] = ""; one.enqueue(m); while (!one.isEmpty()) { String tmp = one.dequeue(); if(tmp == s){ String c = tmp; while (c != m) { cout << c << " "; c = check[c]; } cout << c << endl; return; } for( auto &x: graf){ if( isConnected(tmp,x.first) && check[x.first] == "-" ){ one.enqueue(x.first); check[x.first] = tmp; } } } } int main() { Graph g; g.addEdge("berlin","london"); g.addEdge("budapest","london"); g.addEdge("london","glassgow"); g.addEdge("budapest","moscow"); g.bFS("berlin","moscow"); g.addEdge("london","moscow"); return 0; }
这是输出.第一个是没有“伦敦” – >“莫斯科”边缘,第二个是将该边缘添加到图表中.
berlin london budapest moscow berlin london moscow
以上是大佬教程为你收集整理的c – 寻找BFS的方式全部内容,希望文章能够帮你解决c – 寻找BFS的方式所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。