0

The user will input the number of nodes in a graph followed by an overall "planet" name. then they will input where is the name of the planet and is the number of locations in this planet. Afterwards, lines follow, each of the form: …. indicates the name of a location, indicates the number of neighbors of and …. is a list of the neighbors of .

For example: 3 Venus 4 spaceport 2 beach disco beach 1 bar bar 1 spaceport disco 1 bar Neptune 3 spaceport 1 toy-factory toy-factory 0 weapons_depot 1 weapons_depot Binary-2 2 spaceport 1 zero-one zero-one 1 spaceport

There will always be a spaceport. I then have to start at spaceport and list which nodes cannot be gotten too. This is the part I believe I am Seg faulting on. It will compile then seg fault as it tries to output the nodes.

Here is the code:

#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;
  }
ViscousRandom
  • 179
  • 1
  • 9
  • If it segfaults it will also be able to tell you where (backtrace). Run it in the debugger – gvd Dec 10 '12 at 23:23

1 Answers1

0

You are overwriting num_locations, yet trying to use it to access both dimensions. This means that unless the user enters the same value each time, you will run into a problem as one of the dimensions will go out of bounds.

Keep separate variables for each dimension.

RonaldBarzell
  • 3,822
  • 1
  • 16
  • 23