我想用fscanf_s函式,呼叫txt里面的二維陣列,但是不知道怎么用,求教
//讀權矩陣
int** readWeight(int &n,char* filename)
{
FILE *fp;
fp = fopen(filename,"r");
if(fp == NULL)
{
printf("檔案%s打開時出錯\n",filename);
exit(1);
}
int i,j,**w;
fscanf(fp,"%d",&n);
w = (int**)calloc(n,sizeof(int*));
for(i = 0;i < n;i++)
{
w[i] = (int*)calloc(n,sizeof(int));
}
for(i = 0;i < n;i++)
{
for(j = 0;j < n;j++)
{
fscanf(fp,"%d",&w[i][j]);
}
}
fclose(fp);
return w;
}
不知道怎么改,可能是因為我的權重檔案寫的不對,權重資訊如下
0,2,7,2,5
2,0,3,5,1
7,3,0,4,6
2,5,4,0,3
5,1,6,3,0
uj5u.com熱心網友回復:
權重檔案中的,逗號全部替換成空格試試。uj5u.com熱心網友回復:
試了一下,是沒問題的,但是還是報錯,不知道您了不了解tsp問題,我把代碼附上,結果就是跑不出來,距離矩陣本來是0 2 7 2 5
2 0 3 5 1
7 3 0 4 6
2 5 4 0 3
5 1 6 3 0
但是出現了中斷,所以就把它改為
65535 2 7 2 5
2 65535 3 5 1
7 3 65535 4 6
2 5 4 65535 3
5 1 6 3 65535
改成這個的目的是為了讓每個城市不經過自身
源代碼附上,望大神指點:
#include"stdafx.h"
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<windows.h>
typedef struct tf* cnode;
struct tf {
int currentNode;//當前的節點
int set; //當前的狀態集合,用二進制表示
int pathLength;//當前的路徑值
cnode last;//記錄上一個節點,以保存上一個的資訊
cnode next;//記錄下一個節點
};
struct tsp
{
int count;//當前的城市的個數
cnode* ltf;//記錄struct tf陣列
};
void output1(int *a, int n)
{
int i;
for (i = 0; i < n; i++)
printf("%d ", a[i]);
printf("\n");
}
void output2(int **a, int n)
{
int i, j;
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
printf("%d ", a[i][j]);
}
printf("\n");
}
}
//自定義的冪函式<用于之后的集合的表示>
int mypow(int x, int y)
{
return (int)pow((double)x, (double)y);
}
//判斷元素i是否在set集合中
int inSet(int set, int i)
{
if ((set&mypow(2, i - 1)) > 0)
return 1;
else
return 0;
}
//將元素i插入集合set中
void insertSet(int &set, int i)
{
int p = mypow(2, i - 1);
set = set | p;
}
//洗掉set中的i
void deleteSet(int &set, int i)
{
set = ~(mypow(2, i - 1))&set;
}
//讀權矩陣
int** readWeight(int &n, char* filename)
{
FILE *fp;
errno_t err;
if ((err = fopen_s(&fp, filename, "r")) != 0)
{
printf("檔案%s打開時出錯\n", filename);
exit(1);
}
int i, j, **w;
fscanf_s(fp, "%d", &n);
w = (int**)calloc(n, sizeof(int*));
for (i = 0; i < n; i++)
{
w[i] = (int*)calloc(n, sizeof(int));
}
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
fscanf_s(fp, "%d", &w[i][j]);
}
}
fclose(fp);
return w;
}
//回傳組合數C(n,r)值
int ZuHeShu(int n, int r)
{
int i, c, k, *a;
a = (int*)calloc(n, sizeof(int));
for (i = 0; i < n; i++)
{
a[i] = i + 1;
}
c = 0;
do {
i = -1;
for (k = 0; k < r; k++)
if (a[k] < n - r + k + 1)
i = k;
if (i != -1)
{
a[i]++;
for (k = i + 1; k < r; k++)
a[k] = a[k - 1] + 1;
c++;
}
} while (i != -1);
free(a);
return c + 1;
}
//指定組合c(n,r)中所有可能組合,回傳其轉化后的01格式的int陣列
int* ZuHeArray(int *a0, int n, int r, int sn)
{
int i, c, k, *set, *a;
c = 0;
set = (int*)calloc(sn, sizeof(int));
for (i = 0; i < sn; i++)
{
set[i] = 0;
}
a = (int*)calloc(n, sizeof(int));
for (i = 0; i < n; i++)
{
a[i] = i + 1;
}
int t;
for (i = 0; i < r; i++)
{
t = a[i] - 1;
insertSet(set[c], a0[t]);
}
do {
i = -1;
for (k = 0; k < r; k++)
if (a[k] < n - r + k + 1)
i = k;
if (i != -1)
{
a[i]++;
for (k = i + 1; k < r; k++)
a[k] = a[k - 1] + 1;
c++;
for (k = 0; k < r; k++)
{
t = a[k] - 1;
insertSet(set[c], a0[t]);
}
}
} while (i != -1);
free(a);
return set;
}
//設定最后一個集合
int setLastSet(int n)
{
int i, set;
set = 0;
for (i = 0; i < n; i++)
{
set = set | mypow(2, i);
}
return set;
}
//獲得第row行的set中所有元素集合 ,總元素的個數為n<非set集合中的元素個數>
int* getSetNum(int set, int row, int n)
{
int i, j, *a;
a = (int*)calloc(row, sizeof(int));
j = 0;
for (i = 1; i < n; i++)
{
if (inSet(set, i) == 1)
{
a[j] = i;
j++;
}
if (j == row)
{
break;
}
}
return a;
}
//將集合a轉為二進制的數
int setNumSet(int *a, int n)
{
int set, i, t;
set = 0;
for (i = 0; i < n; i++)
{
t = a[i] - 1;
set = set | mypow(2, t);
}
return set;
}
//將set[n]中的第i個點洗掉
int* dropOneNode(int*set, int i, int n)
{
int * s, j, k;
s = (int*)calloc(n - 1, sizeof(int));
k = 0;
for (j = 0; j < n; j++)
{
if (j != i)
{
s[k] = set[j];
k++;
}
}
return s;
}
//查找上一節點
cnode findNode(int row, cnode p, struct tsp circle)
{
cnode phead;
phead = (cnode)malloc(sizeof(*phead));//必須分配空間
phead = circle.ltf[row];
cnode q;
q = (cnode)malloc(sizeof(*q));
q = phead;
while (phead != NULL)
{
q = phead;
if ((q->currentNode == p->currentNode) && (p->set == q->set))
{
break;
}
phead = phead->next;
}
return q;
}
//設定上一節點
void setLastNode(cnode p, int row, struct tsp circle, int **w)
{
int start = p->currentNode;
cnode* clast;
int i, n;
n = circle.count;
int *setNum;
//將節點p中的set轉為相應的int型陣列
setNum = getSetNum(p->set, row, n);
clast = (cnode*)calloc(row, sizeof(cnode));
//設定所有關聯的上一級的接點
for (i = 0; i < row; i++)
{
int *dSet = dropOneNode(setNum, i, row);
int sDNum;
if (row == 1)
{
sDNum = 0;
}
else
{
sDNum = setNumSet(dSet, row - 1);
}
clast[i] = (cnode)malloc(sizeof(*clast[i]));//必須有的陳述句
clast[i]->currentNode = setNum[i];
clast[i]->last = NULL;
clast[i]->next = NULL;
clast[i]->set = sDNum;
clast[i] = findNode(row - 1, clast[i], circle);//之后測驗這個函式
}
int minlength, minlabel;
cnode q;
q = clast[0];
minlength = w[start][q->currentNode] + q->pathLength;
minlabel = 0;
for (i = 1; i < row; i++)
{
q = clast[i];
int len = w[start][q->currentNode] + q->pathLength;
if (minlength > len)
{
minlength = len;
minlabel = i;
}
}
p->last = clast[minlabel];
p->pathLength = minlength;
}
//設定集合,即已知起點,列舉所有可能狀態集合
int* setSet(int row, int start, int n, int &sn)
{
int *set, i, j, *a;
sn = ZuHeShu(n - 2, row);
//集合a中存放V中所用可能取得的點的下標
a = (int*)calloc(n - 2, sizeof(int));
i = 0;
for (j = 1; j < n; j++)
{
if (j != start)
{
a[i] = j;
i++;
}
}
set = ZuHeArray(a, n - 2, row, sn);
free(a);
return set;
}
void tsp(char *filename)
{
int **w, n;
//從檔案中讀取路徑權值矩陣資訊
w = readWeight(n, filename);
//資料結構:使用的是鏈接結構
//每一層對應一行,每一層中所有的節點依次排列
struct tsp circle;
circle.count = n;
circle.ltf = (cnode*)calloc(n - 1, sizeof(cnode));
int i, j;
for (i = 0; i < n - 1; i++)
{
circle.ltf[i] = NULL;
}
cnode p, q;
q = (cnode)malloc(sizeof(*q));
//添加第一行
for (i = 1; i < n; i++)
{
p = (cnode)malloc(sizeof(*p));
p->currentNode = i;
p->last = NULL;
p->next = NULL;
p->pathLength = w[i][0];
p->set = 0;
if (circle.ltf[0] == NULL)
{
circle.ltf[0] = p;
}
else
{
q->next = p;
}
q = p;
}
int *sSet, sn, ts;
//添加其余幾行
for (i = 1; i < n - 1; i++)
{
for (j = 1; j < n; j++)
{
sSet = setSet(i, j, n, sn);
for (ts = 0; ts < sn; ts++)
{
p = (cnode)malloc(sizeof(*p));
p->currentNode = j;
p->set = sSet[ts];
setLastNode(p, i, circle, w);//是i不是j
if (circle.ltf[i] == NULL)
{
circle.ltf[i] = p;
}
else
q->next = p;
q = p; free(sSet);
}
}
}
//求解最后的f0(v0,{v1,v2,...vn-1});
cnode last;
last = (cnode)malloc(sizeof(*last));
last->currentNode = 0;
last->set = setLastSet(n - 1);
setLastNode(last, n - 1, circle, w);
int *path;
path = (int*)calloc(n, sizeof(int));
i = 0;
p = last;
while (p != NULL)
{
cnode p1 = p;
path[i] = p->currentNode + 1;
i++;
p = p->last;
}
printf("最短路的路徑為: ");
for (i = 0; i < n; i++)
{
printf("%d -> ", path[i]);
}
printf("%d \n", 1);
printf("最短路徑的長度為:%d\n", last->pathLength);
}
void main()
{
tsp("da.txt");
system("pause");
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/53497.html
