【发布时间】:2014-12-06 22:47:35
【问题描述】:
我正在做一个解决流浪推销员问题的课程项目,基本上就是TSP,只是你不回源。我采取的方法是找到一组顶点的所有可能排列,然后迭代地计算长度并比较它们以找到最小的。我知道这不是最好的方法,但这是我们教授想要的。
当我运行下面的代码时,它工作正常,并且当输入数组小于 7 X 7 时给了我正确的答案。但是当它的 7 X 7 时,它返回“总线错误:10”,如果大小是 12 x 12,它返回“分段错误:11”。我花了几个小时查找这些问题,但无法弄清楚出了什么问题。我不是 C 编程方面的专家,老实说,我真的对指针感到困惑。非常感谢您的帮助,非常感谢!
哦,抱歉代码乱七八糟。
#include <stdio.h>
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
#include<stdlib.h>
int x = 0;
int a[];
void swap (int v[], int i, int j) {
int t;
t = v[i];
v[i] = v[j];
v[j] = t;
}
/* recursive function to generate permutations */
int* perm (int v[], int n, int i) {
/* this function generates the permutations of the array
* from element i to element n-1
*/
int j;
/* if we are at the end of the array, we have one permutation
* we can use (here we print it; you could as easily hand the
* array off to some other function that uses it for something
*/
if (i == n) {
for (j=0; j<n; j++){ a[x] = v[j]; x++;} // printf ("%d ", v[j]);
// printf ("\n");
}
else
/* recursively explore the permutations starting
* at index i going through index n-1
*/
for (j=i; j<n; j++) {
/* try the array with i and j switched */
swap (v, i, j);
perm (v, n, i+1);
/* swap them back the way they were */
swap (v, i, j);
}
return a;
}
int fact(int n){
if(n==1){
return 1;
}
else{
return n * fact(n-1);
}
}
int findShortestPath(int **v , int length){
int pathArrayMultiplier = 0;
int ShortestPathLength = 99999;
printf("Called");
int arrayOfVertices[length-1];
for(int i=0 ; i<length-1 ; i++){
arrayOfVertices[i] = i+2;
}
int n = fact(length-1);
bool doBreak = false;
int pathArray[length-1];
//printf(" Called 3");
printf(" %d" , n);
int* Answer;
Answer = malloc(sizeof(int *));
Answer = perm(arrayOfVertices , length-1 , 0);
printf("Called 4");
int j =-1;
for(int i=0 ; i< n*(length-1) ; i++){
doBreak = false;
j++;
printf("%d " , *(Answer + i));
pathArray[j] = *(Answer+i);
if(j == length-2)
{
j = -1;
// Check for negative values. If any value is negative, disregard path
int checklength = *((int *)v + 0 *length + (pathArray[0]-1));
if(checklength < 0){
printf("First check called");
continue;}
for(int i =0 ; i<length-2 ; i++){
if(*((int *)v + (pathArray[i]-1) * length + (pathArray[1 + i]-1)) < 0){
doBreak = true;
printf("Second Check called");
break;}
}
if(doBreak) { pathArrayMultiplier++; continue;}
printf("\n");
int pathLength = *((int *)v + 0 *length + (pathArray[0]-1));
for(int i =0 ; i<length-2 ; i++){
pathLength = pathLength + *((int *)v + (pathArray[i]-1) * length + (pathArray[1 + i]-1));}
printf("Path Length is %d\n" , pathLength);
if(pathLength < ShortestPathLength) { ShortestPathLength = pathLength;}
}
}
printf("\n\n Shortest Path Length is %d \n" , ShortestPathLength);
return ShortestPathLength;
}
int main () {
int len = 5;
printf("Array is initialized");
int v[7][7] = {0,7,-1,10,1,-1,-1,7,0,-1,-1,10 ,-1 ,1,-1,-1,0,10,1,10,-1,10,-1,10, 0,8,-1,-1,-1,10,1,8,0,10,-1,-1,-1,10,-1,10,0,70,-1,1,-1,-1,-1, 70 , 0};
printf("Array is initialized");
int **realArrayPointer = v;
findShortestPath(realArrayPointer, 7);
return 0;
}
【问题讨论】:
-
您的标题对我来说听起来不对,您在编译或运行程序时出错?此外,您的程序看起来很糟糕,请在此处发布之前正确缩进您的代码。不要对乱码感到抱歉,把它清理干净,这将有助于你自己理解它。
-
当您的代码无法编译时,很难出现总线错误。
int **realArrayPointer = v;出现类型不匹配错误。那是因为int**不是与int[N][M]同义,无论您认为它们是什么方法,它都是错误的。您还至少有一个内存泄漏(第 98,99 行,Answer = ...),我只能假设其他错误,但我不再关注。 -
“BUS 错误”听起来并不是特别面向软件。
标签: c pointers segmentation-fault int bus