冰楓論壇

標題: C語言-最短路徑樹(MST)問題 [打印本頁]

作者: 呂晨    時間: 2021-9-29 08:54
標題: C語言-最短路徑樹(MST)問題
本帖最後由 呂晨 於 2021-9-29 18:39 編輯

讀txt檔
1.PNG
我有查到兩種方法,一種是一個點一個點找,另一種拿上面這個file來解說,總共有11個點先扣掉起點N1,剩下10個點,各自找到最近的相鄰點成為一個group,會變成有5個gropu,這五個group再去找最近的相鄰group,連接成為新的group。
3.PNG
第二種會變成有分支出去,而第一種我已經寫出來了。
有兩個問題想詢問
第一個問題是第二種要怎麼去構思
第二個問題是把結果輸出後,會跑出很多空格,我是用memset把字串都變成'\0',在小黑窗上面不會顯示這些,但輸出到txt後會跑出來
4.PNG
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. #include<math.h>
  5. #pragma warning( disable : 4996 )
  6. #define N 13

  7. struct point
  8. {
  9.         char* name;
  10.         int x;
  11.         int y;
  12.         float howlong;
  13. }*matrix;

  14. void findpoint(struct point* matrix, int line)
  15. {
  16.         struct point temp;
  17.         int times = 1, i = 0;
  18.         int  x = 0, y = 0;
  19.         float v = 20000;
  20.        
  21.         while (line>=times)
  22.         {
  23.                 printf("YES");
  24.                 for (i = times ; i < line; i++)
  25.                 {
  26.                         matrix[i].howlong = pow(matrix[i].x - matrix[i-1].x, 2) + pow(matrix[i].y - matrix[i-1].y, 2);
  27.                 }
  28.                 for (x = times; x < line; x++)
  29.                 {
  30.                         for (y = times; y < line - 1; y++)
  31.                         {
  32.                                 if (matrix[y].howlong > matrix[y + 1].howlong)
  33.                                 {
  34.                                         temp = matrix[y];
  35.                                         matrix[y] = matrix[y + 1];
  36.                                         matrix[y + 1] = temp;
  37.                                 }
  38.                         }
  39.                 }
  40.                 printf("(%s,%s) Length = %f", matrix[times - 1].name, matrix[times].name, sqrt(matrix[times].howlong));
  41.                 times++;
  42.         }
  43. }


  44. int main()
  45. {
  46.         int i, line=0, tmp;
  47.         char name[N];
  48.         char value[N];
  49.         printf("enter the file name:(.txt)");
  50.         scanf("%s", &name);

  51.         FILE* fp;
  52.         fp = fopen(name, "r");
  53.         if (fp == NULL)
  54.         {
  55.                 puts("error");
  56.         }
  57.         while ((i = fgetc(fp)) != EOF)
  58.         {
  59.                 if (i == '\n') line++;
  60.         }
  61.        
  62.         fseek(fp, 0L, SEEK_SET);
  63.         i = 0;
  64.         matrix = (struct point*)malloc(line * sizeof(struct point));
  65.         while ((fgets(value, sizeof(value), fp)) != NULL)
  66.         {
  67.                 char* token = strtok(value, "(");
  68.                 matrix[i].name = (char*)malloc(sizeof(char) * N);
  69.                 strcpy(matrix[i].name, token);
  70.                 token = strtok(NULL, ",");
  71.                 tmp = atoi(token);
  72.                 matrix[i].x = tmp;
  73.                 token = strtok(NULL, ")");
  74.                 tmp = atoi(token);
  75.                 matrix[i].y = tmp;
  76.                 i++;
  77.         }
  78.         findpoint(matrix, line);
  79.         free(matrix);
  80. }
複製代碼

2.PNG (4.76 KB, 下載次數: 1)

2.PNG






歡迎光臨 冰楓論壇 (https://bingfong.com/) Powered by 冰楓