これが私の初めてのプログラミング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で始まるかそのインターフェイス、与えられた、私にはかなり混乱して
、Iを行いますBFSは比較になり新しい関数を作る、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++で? –