广度优先搜索问题 C++

发布于 2024-11-28 11:12:51 字数 2358 浏览 1 评论 0原文

这是我第一次编写 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 nameint 组成的结构玩家。有人可以向我解释一下如何制作 BFS 吗?

提前致谢!

编辑:

我了解 BFS 的算法,但由于我只编写过 C 语言,因此理解 OO 编程对我来说非常困惑,考虑到该接口,我从哪里开始使用这个 BFS,我是否创建一个新函数来使 BFS将起始字符串目标字符串进行比较

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;
}

This is my first time programming C++ and I've been asked to code a breadth first search where given this class

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 is a struct consisting of string name and int players. Could someone explain to me how I'd go about making the BFS?

Thanks in advance!

EDIT:

I understand the algorithm for BFS, but since I've only ever programmed C, understanding OO programming is quite confusing to me, given that interface, where do I start with this BFS, do I make a new function which makes the BFS comparing, the start string with the 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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

枯叶蝶 2024-12-05 11:12:52

广度优先搜索就是问两个问题

  1. 我现在处于什么状态?
  2. 从这里我可以到达哪些州?

这个想法是有一个初始状态并不断问自己这两个问题,直到

  1. 没有更多的状态离开。
  2. 我已经到达目的地状态。

BFS 通常使用一个队列,您只需将找到的任何新状态添加到其中,每当您想要处理新状态并将任何新状态添加到队列末尾时,只需从队列前面弹出即可。

Breadth First search is all about asking 2 questions

  1. What state am I at right now?
  2. What states can I get to from here?

The idea is to have an initial state and continuously ask yourself these 2 questions until

  1. No more states left.
  2. I have reached the destination state.

BFS usually uses a Queue to which you simply add any new states you find and simply pop from the front of the queue whenever you want to process a new state and add any new states to the end of the queue.

故人爱我别走 2024-12-05 11:12:52

路由类只是一种存储使用 BFS 找到的路由的机制。至少我是这么解释的。 BFS 算法将是一个独立的函数,它将在适当的时间调用路由类的方法。由于 BFS 需要维护有关多个路由的信息,因此它必须在某种列表或队列中创建多个路由对象。 BFS 的每一步都会从队列中取出一个路由对象,复制它并对其调用 addConnect 以移动到下一个位置,然后将其放回队列中。重复此操作,直到到达目的地,然后从 BFS 函数返回表示最短路径的路由对象。

无论如何,类似的事情。

The route class is only a mechanism to store the routes that you find using BFS. At least that's how I'm interpreting it. The BFS algorithm will be a stand alone function that will call methods of the route class at appropiate times. Since a BFS needs to maintain information about multiple routes it will have to create multiple route objects in some sort of list or queue. Each step of the BFS will take one route object from the queue, copy it and call addConnect on it to move to the next location, then place it back on the queue. Repeat until you get to your destination, then return the route object representing the shortest path from your BFS function.

Something like that anyway.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文