- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
这是我第一次使用 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 语言,理解 OO 编程对我来说非常困惑,考虑到该接口(interface),我从哪里开始这个 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;
}
最佳答案
广度优先搜索就是问 2 个问题
想法是有一个初始状态,不断问自己这2个问题,直到
BFS 通常使用一个队列,您只需将找到的任何新状态添加到其中,只要您想要处理一个新状态,只需从队列的前面弹出,并将任何新状态添加到队列的末尾即可。
关于c++ - 广度优先搜索题 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6990825/
我有两个关于这段代码的问题。 double*** pdata 和 int*** pmask 是什么意思?指向指针的指针?为什么或何时需要这样做? int 和 double 是不同的类型,double*
谁能用英文解释一下这是怎么回事? std::vector cats; //I get that cats is a vector of Cat objects if (std::find(cats.b
在C中,下列声明有区别吗: float DoSomething( const float arr[] ); 对比 float DoSomething( const float* arr ); 一个比另
我到 question 36我认为这很简单。像往常一样,我显然错了。我正在尝试在 Python 中执行此操作(因为我不知道 Python)。我的代码如下。我得到 19 作为输出,这显然是不正确的。我不
我已经通读了 MSDN 上的 Winsock2 文档,但如果有人能提供帮助,我仍然需要澄清一些事情。 我计划做一些类似于您在使用 WSAAsyncSelect() 时获得的设置,但使用一个单独的线程。
#include int main () { int *p = (int *)malloc((100*sizeof(int))); p++; free(p); /* do some
我想提供未知的“对象”并返回其成员之一的值。在 C# 中需要响应。 一般来说,我想我正在寻找这个方法的代码公共(public)静态对象 GetObjectMemberValue (object myO
由异常准确的 AI 提供支持的 20 个问题的简单在线游戏。 他们怎么猜得这么好? 最佳答案 您可以将其视为二进制搜索算法。在每次迭代中,我们都会提出一个问题,该问题应该会消除大约一半的可能单词选择。
拜托,有人可以解释一下吗: 如果文档说 STL std::vector finding element speed performace = O(ln(n)),这是什么意思。 O(ln(n)) - 什
我正在尝试通过遵循 Microsoft 为 ADSI API 和 Windows-RS crate 发布的 c++ 示例来使用 Rust 的事件目录。我不太明白这里发生了什么: https://doc
这是处理具有重复元素的单个列表的 nieve 案例,我在处理一些嵌套列表时遇到了麻烦,所以我想先写简单的案例。 所以我有: (defn packDuplicatesIntoLists [lis
我是新来的。我正在尝试解决此练习 Problem 18只是为了加强我的解决能力。我已经编码了答案。该任务要求“在 1,000,000 以下的质数中,有多少个数位之和等于两周中的天数?” (两周是 14
我正在尝试对POCO类中的某些字段进行索引,并将某些属性装饰为“忽略= true”,并且这些字段不应被索引,而应该被存储。我希望这些字段出现在搜索结果中,但不应作为索引。 我正在尝试对应索引的几个字段
我是编码的新手,正在尝试通过完成 Project Euler 问题来学习 Swift。我似乎有导致大量错误的不同版本的 Swift 代码。如果您对我的问题的格式有任何建议以供将来引用,请告诉我,谢谢。
对于problem statement在 google codejam 2008:第 1A 轮问题 3 In this problem, you have to find the last three
我是一名优秀的程序员,十分优秀!