我正在尝试实现一个 for 循环来初始化网格形状的图形,包括对角线。基本上,我有一个数组,它使用我想在图中复制的值进行初始化。所以我有一个嵌套的 for 循环,它有几个 if 语句。if 语句用于处理特殊情况,即索引 1,1 处的元素只有 3 个邻居。
我知道我的图形函数有效,因为如果我手动初始化它,它不会出现段错误并打印正确的 BFS,但是我的循环段错误。请看一下:
图表类:
Graph::Graph(int V)
{
this->V = V;
adj = new list<int>[V];
}
void Graph::addEdge(int v, int w)
{
adj[v].push_back(w); // Add w to v’s list.
}
void Graph::BFS(int s, int d)
{
// Mark all the vertices as not visited
bool *visited = new bool[V];
int trail[V];
for(int i = 0; i < V; i++){
visited[i] = false;
trail[i] = -1;
}
// Create a queue for BFS
list<int> queue;
// Mark the current node as visited and enqueue it
visited[s] = true;
queue.push_back(s);
// 'i' will be used to get all adjacent vertices of a vertex
list<int>::iterator i;
while(!queue.empty())
{
// Dequeue a vertex from queue and print it
s = queue.front();
if(s == d){
break;
}
else
queue.pop_front();
// Get all adjacent vertices of the dequeued vertex s
// If a adjacent has not been visited, then mark it visited
// and enqueue it
for(i = adj[s].begin(); i != adj[s].end(); ++i)
{
if(!visited[*i])
{
visited[*i] = true;
queue.push_back(*i);
trail[*i] = s;
}
}
}
int x = d;
while(x != -1){
cout<<x<<endl;
x = trail[x];
}
}
在主程序中:
int num = 2;
int arr[num+1][num+1];
int x = 1;
for(int i = 1; i<=num; i++){
for(int j = 1; j<= num; j++){
arr[i][j] = x;
cout<<x<<" ";
x++;
}
cout<<endl;
}
int max = 2;
Graph g(max+1);
for(int row = 1; row <= max; row++){
for(int col = 1; col <= max; col++){
if(row == 1 && col == 1){
g.addEdge(arr[row][col],(arr[row][col] +1));
g.addEdge(arr[row][col],(arr[row][col] +max));
g.addEdge(arr[row][col],(arr[row][col] + max+1));
}
else if(row ==1 && col == max){
g.addEdge(arr[row][col],(arr[row][col]-1));
g.addEdge(arr[row][col],(arr[row][col]+max));
g.addEdge(arr[row][col],(arr[row][col]+max-1));
}
else if(row == max && col == max){
g.addEdge(arr[row][col],(arr[row][col]-1));
g.addEdge(arr[row][col],(arr[row][col]-max));
g.addEdge(arr[row][col],(arr[row][col]-max-1));
}
else if(row == max && col == 1){
g.addEdge(arr[row][col],(arr[row][col]-max));
g.addEdge(arr[row][col],(arr[row][col]-max+1));
g.addEdge(arr[row][col],(arr[row][col]+1));
}
else if(row == max){
g.addEdge(arr[row][col],(arr[row][col]-1));
g.addEdge(arr[row][col],(arr[row][col]+1));
g.addEdge(arr[row][col],(arr[row][col]-max));
g.addEdge(arr[row][col],(arr[row][col]-max-1));
g.addEdge(arr[row][col],(arr[row][col]-max+1));
}
else if(col == max){
g.addEdge(arr[row][col],(arr[row][col]-1));
g.addEdge(arr[row][col],(arr[row][col]-max));
g.addEdge(arr[row][col],(arr[row][col]+max));
g.addEdge(arr[row][col],(arr[row][col]-max-1));
g.addEdge(arr[row][col],(arr[row][col]+max-1));
}
else if(col == 1){
g.addEdge(arr[row][col],(arr[row][col]+1));
g.addEdge(arr[row][col],(arr[row][col]+max));
g.addEdge(arr[row][col],(arr[row][col]-max));
g.addEdge(arr[row][col],(arr[row][col]-max+1));
g.addEdge(arr[row][col],(arr[row][col]+max+1));
}
else if(row == 1){
g.addEdge(arr[row][col],(arr[row][col]-1));
g.addEdge(arr[row][col],(arr[row][col]+1));
g.addEdge(arr[row][col],(arr[row][col]+max));
g.addEdge(arr[row][col],(arr[row][col]+max-1));
g.addEdge(arr[row][col],(arr[row][col]+max+1));
}
else{
g.addEdge(arr[row][col],(arr[row][col]+1));
g.addEdge(arr[row][col],(arr[row][col]-1));
g.addEdge(arr[row][col],(arr[row][col]+max));
g.addEdge(arr[row][col],(arr[row][col]-max));
g.addEdge(arr[row][col],(arr[row][col]-max-1));
g.addEdge(arr[row][col],(arr[row][col]-max+1));
g.addEdge(arr[row][col],(arr[row][col]+max-1));
g.addEdge(arr[row][col],(arr[row][col]+max+1));
}
}
}
注意:我希望我的图形顶点从 1 开始,而不是从 0 开始。这就是为什么我的矩阵中有一个额外的行和列的原因。此外,我的图表需要在两个方向上添加一条边,因此它将是 1--->0 和 0--->1。