首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >查找有向图c++分段错误时出错

查找有向图c++分段错误时出错
EN

Stack Overflow用户
提问于 2012-12-11 07:20:33
回答 1查看 104关注 0票数 0

用户将输入图形中的节点数,后跟一个整体的"planet“名称。然后,他们将输入where是行星的名称,是这个行星上的位置数。然后,每种形式的代码行如下:…。表示位置的名称,表示和…的邻域数量。是的邻居列表。

例如:3个金星4个spaceport 2个海滩迪斯科海滩1个酒吧1个spaceport disco 1 bar海王星3个spaceport 1玩具工厂玩具工厂0 weapons_depot 1 weapons_depot Binary-2 2 spaceport 1 0-1 0-1 spaceport 1 spaceport

总会有一个太空港。然后,我必须从spaceport开始,并列出无法获取的节点。这就是我认为我的Seg错误的部分。当它尝试输出节点时,它将编译然后seg tries。

代码如下:

代码语言:javascript
复制
#include <iostream>
#include <map>
#include <vector>
using namespace std;
bool path(int x, int y, vector<vector<bool> > graph);

int main()
{ 
  int num_planets; 
  cin>>num_planets; 

  for (int m=0; m<num_planets; m++) 
  { 
    string planet; 
    int num_locations; 

    cin>>planet; 
    cin>>num_locations; 
    map<string, int> m_planet; 
    vector<vector<bool> > graph; 
    graph.resize(num_locations);

    for (int n=0; n<num_locations; n++) 
    { 
      graph[n].resize(num_locations); 
    } 
    for(int k=0; k<num_locations; k++) 
    { 
      for (int j=0; j<num_locations; j++) 
      {
        graph[k][j] = false; 
      } 
    } vector<vector<string> > connections;

    vector<string> places; 
    for (int o=0; o<num_locations; o++) 
    { 
      string place;

      cin>>place; 
      places.push_back(place); 
      m_planet[place] = o; 
      int edges;

      cin>>edges; 
      connections.resize(num_locations);
      connections[o].resize(edges); 

      for (int p=0; p<edges; p++) 
      { 
        string connect;
        cin>>connect; 
        connections[o][p]=connect; 
      } 
    } 

    for (int q=0; q<num_locations; q++) 
    { 
      for (int r=0; r<connections[q].size(); r++) 
      { 
        int from, to; 
        from = m_planet[places[q]]; 
        to = m_planet[connections[q][r]]; 
        graph[from][to] =true; 
      } 
    } 

    cout<<"In planet "<<planet<<":"<<endl; 

    int num_paths = 1; 
    for(int s=1; s<num_locations; s++) 
    { 
      bool route; 
      route = path(0, s, graph); 

      if(route == false) 
      { 
        cout<<places[s]<<"unreachable from the#"
          <<places[0]<<"."<<endl; 
      } 

      else 
      { 
        num_paths++; 
      } 
    } 

    if (num_paths == num_locations) 
    { 
      cout<<"All locations reachable from the#"<<places[0]<<"."<<endl; 
    } 
  } 

  return 0;
}

  bool path(int x, int y, vector<vector<bool> > graph)
  { 
    for (int m=0; m<graph[x].size(); m++) 
    { 
      if(graph[x][m] == true) 
      { 
        if (graph[x][m] == y) 
        { 
          return true; 
        } 

        else 
        {
          return path(m, y, graph); 
        } 
      } 
    } 

    return false;
  }
EN

回答 1

Stack Overflow用户

发布于 2012-12-11 07:26:22

您正在覆盖num_locations,但却试图使用它来访问这两个维度。这意味着,除非用户每次都输入相同的值,否则您将遇到问题,因为其中一个维度将超出界限。

为每个维度保留单独的变量。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13811000

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档