0
用戶將輸入圖形中的節點數,然後輸入總體「行星」名稱。那麼他們將輸入 這裏是行星的名稱,並且是這個星球上的位置數量。 然後,行後面,每個形式: ...。 指示位置的名稱,指示和...的鄰居的數量。是一個鄰居列表。通過有向圖查看錯誤C++分割錯誤
例如:金星4 航空基地2海灘迪斯科 海灘1巴 條1個航空基地 迪斯科1巴 海王星3 航空基地1玩具廠 玩具廠0 weapons_depot 1 weapons_depot 選擇Binary- 2 2 航空基地1 0-1 0-1 1航空基地
總是會有一個航空基地。然後我必須從太空港開始,列出哪些節點不能得到。這是我認爲我是Seg錯誤的部分。它會在嘗試輸出節點時編譯seg故障。
下面是代碼:
#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;
}
如果它segfaults它也將能夠告訴你在哪裏(backtrace)。在調試器中運行它 – gvd