2012-12-10 2 views
0

사용자는 그래프에서 노드 수를 입력 한 다음 전체 "행성"이름을 입력합니다. 그때 그들은 을 입력 할 것입니다. 여기서는 행성의 이름이고이 행성의 위치 수입니다. 그 다음에는 각 줄이 따라오고 각 형식은 ...입니다. 은 위치 이름을 나타내고,의 이웃 수를 나타냅니다. 의 이웃 목록입니다. 예를 들어유향 그래프를 통한 오류 C++ 분할 오류

: 금성 4 우주 정거장 2 해변 디스코 비치 한 바 바 1 우주 정거장 디스코 1 줄 해왕성 장난감 공장 0 weapons_depot 1 을 weapons_depot 3 우주 정거장 1 장난감 공장 Binary- 2 2 스페이스 포트 1 제로 제로 원 1 스페이스 포트

항상 스페이스 포트가 있습니다. 나는 우주 정거장에서 시작해야하고 어느 노드도 구할 수없는 것을 열거해야한다. 이것은 제가 세그가 잘못하고 있다고 믿는 부분입니다. 그러면 노드를 출력하려고 할 때 오류가 발생하여 컴파일됩니다. 당신은 NUM_LOCATIONS을 덮어, 아직 두 차원에 액세스하는 데 사용하려는

#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; 
    } 
+0

segfaults를 사용하면 어디에서 (백 트레이스) 말할 수 있습니다. 디버거에서 실행 – gvd

답변

0

: 여기

는 코드입니다. 즉, 사용자가 매번 동일한 값을 입력하지 않으면 차원 중 하나가 범위를 벗어날 때 문제가 발생합니다.

각 치수에 대해 별도의 변수를 유지하십시오.