-4

我正在尝试编写 Prim 的算法。但是,当我编译时,出现以下错误:57,59,60,62,63,65

显然这与我的数组有关......我只是不知道究竟是什么。我觉得这与我试图弄清楚但失败的指针有关。

int map[][7] 是我用来跟踪我的初始图形的全局数组。

编辑-这里要求的是所有代码

#include <iostream>

using namespace std;

void performPrims(int);
void print(int *);

// Global Variables
int nodes = 7;
int cur   = 0;
int cost  = 0;

int map[][7] =  
   {{0,3,6,0,0,0,0},
    {3,0,7,0,0,12,0},
    {6,7,0,10,13,8,0},
    {0,0,10,0,0,0,5},
    {0,0,13,0,0,9,4},
    {0,12,8,0,9,0,11},
    {0,0,0,5,4,11,0}};

int main()
{
// Initializations

srand(time(NULL)); 
int S = rand() % 7; // Create an arbitrary starting point

// Perform Prim's Algorithm starting with S

performPrims(S);

return 0;
}

void performPrims(int S)
{
int low = 100;

// open[] is used to keep track of what vertices are available
int open [] = {1,2,3,4,5,6,7};

// closed [] is used to keep track of what vertices have been reached and in what order
int closed [] = {0,0,0,0,0,0,0};

open [S] = 0;       // Make our starting node unavailable
closed [cur] = S+1; // Record our starting node in the path
cur++;

while (cur < 7)
{
    int i=0;
    while (i<nodes)
    {
        int k=0;
        while (k<nodes)
        {
            if (*map[*close[i]-1][k] > 0 && *map[*close[i]-1][k] < low)
            {
                low = map[close[i]-1][k];
                close [cur] = k+1;
            }
            map[close[cur-1]-1][close[cur]-1] = 0;
            map[close[cur]-1][close[cur-1]-1] = 0;
            cost += low; low = 100;
            open[close[cur]-1] = 0;
            k++;
        }
        i++;
    }
    cur++;
}
print(closed);
}

void print(int *closed)
{
cout<<"The path taken was ";
for (int i=0; i<nodes; i++)
    cout<<closed[i]<<" "<<endl;
cout<<"\nThe cost is "<<cost<<endl;
}

错误

有几个不同的:

错误:指向算术(多行)中使用的函数的指针错误:数组下标(多行)的无效类型'int [7] [7] [int(*)(int)]'错误:分配只读位置(第 60 行)错误:无法在赋值中将“int”转换为“int ()(int)”(第 60 行)

4

1 回答 1

3

你拼错了closed。你输入close了。

close用替换所有实例closed

另外,添加#include <cstdlib>到文件的顶部。

于 2012-05-01T20:53:52.867 回答