这是我第一次编程C++,我一直在问哪里给这个类广度优先搜索问题C++
class route {
friend ostream& operator<<(ostream& os, const route& p);
public:
route(const string& startPlayer);
int getLength() const { return links.size(); };
void addConnect(const sport& s, const string& player);
void removeConnect();
const string& getLastPlayer() const;
private:
struct Connect {
sport s;
string player;
Connect() {}
Connect(const sport& s, const string& player) : s(s), player(player) {}
};
string startPlayer;
vector<Connect> links;
};
sport
编写一个广度优先搜索是由string name
和int players
一个结构。有人能向我解释我将如何去做BFS吗?
在此先感谢!
编辑:
我了解BFS算法,但因为我只过编程,C,理解面向对象编程是相当混乱给我,因为界面,我从哪里开始这个BFS,我做使一个新的功能,这使得BFS相比,start string
与target string
namespace {
string promptForSPlayer(const string& prompt, const spdb& db)
{
string response;
while (true) {
cout << prompt << " [or <enter> to quit]: ";
getline(cin, response);
if (response == "") return "";
vector<sport> splist;
if (db.getsplist(response, splist)) return response;
cout << "It's not here: \"" << response << "\" in the sports database. "
<< "Please try again." << endl;
}
}
}
int main(int argc, char *argv[])
{
if (argc != 2) {
cerr << "Usage: sports" << endl;
return 1;
}
spdb db(argv[1]);
if (!db.good()) {
cout << "Failed to properly initialize the spdb database." << endl;
cout << "Please check to make sure the start files exist and that you have permission to read them." << endl;
exit(1);
}
while (true) {
string start = promptForSplayer("Player", db);
if (start == "") break;
string target = promptForSplayer("Another Player", db);
if (target == "") break;
if (start == target) {
cout << "Good one. This is only interesting if you specify two different people." << endl;
} else {
// replace the following line by a call to your generateShortestPath routine...
cout << endl << "No path between those two people could be found." << endl << endl;
}
}
return 0;
}
该算法在伪代码中的布局非常好:http://en.wikipedia.org/wiki/Breadth-first_search – RageD
我还没有尝试过任何东西,因为我很困惑,因为如何开始 – FHr
编程困惑在C++中? –