• 邻接矩阵作为主要存储结构


    行政区划中 每个景点的信息有{编号,城市名字,地理位置}

    初步设想是

    1 struct city
    2 {
    3     int num;
    4     char name;
    5     char location;
    6     char neighbour[];
    7 };


    初步设想是 在字符串数组存放相邻的城市,然后邻接矩阵每一个metro[i][j]可以从第 i 个城市的neighbour[] 查找是否有与第j个城市相同名字的城市。

    因为已经有了染色算法,所以我们只要在原有基础上将merto的矩阵获取方式改变一下就好。

    首先先输入N个城市编号 名字 地址,之后是neighbour的N元素的数组。

    源代码如下:

      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 
      4 /*定理:任何平面地图可以使用4种颜色给每个不同的城市着色,而保证相邻的城市着不同的颜色。 */
      5 
      6 /*思路:把地图上的每个城市抽象为一个点,并给每个城市编号,,相邻的城市之间用直线连接。据此做出邻接矩阵,若第i个城市与第j个城市相邻,则metro[i][j]=1,否则metro[i][j]=0。  */
      7 
      8 /*算法:按照编号从小到大的顺序检查每个城市,对每个城市从1到4使用4种颜色着色,若当前颜色可用(即不与相邻城市颜色相同),则着色;否则测试下一种颜色。   */
      9 
     10 #define   N   5
     11 struct city_Info
     12 {
     13     int num;
     14     char name[20];
     15     char location[20];
     16     int  neighbour[N];
     17 } city[N];
     18 
     19 /*录入各个城市信息*/
     20 /*函数作用:产生城市信息*/
     21 
     22 void inputInfo(struct city_Info * city,int city_Num)
     23 {
     24 
     25     city[city_Num].num=city_Num+1;
     26     printf("please input the name of %d city \n",city_Num+1);
     27     scanf("%s",city[city_Num].name);
     28     printf("please input the location of %d city\n ",city_Num+1);
     29     scanf("%s",city[city_Num].location);
     30     printf("请输入城市相邻关系 相邻为1 不相邻为0\n");
     31     int count=0;
     32     for(count=0; count<N; count++)
     33     {
     34         printf("请输入此城市与第%d城市的相邻关系",count+1 );
     35         scanf("%d",&city[city_Num].neighbour[count]);
     36     }
     37 }
     38 
     39 
     40 
     41 
     42 int   check(int   metro[N][N],int   r_color[N],int   current)
     43 {
     44     /*测试当前着色方案是否可行*/
     45     int   j;
     46     for(j=0; j <current; j++)
     47         if(metro[current-1][j]==1&&r_color[j]==r_color[current-1])
     48             return   0;/*城市相邻且颜色相同*/
     49     return   1;
     50 }
     51 
     52 void   drawColour(int   metro[N][N],int   r_color[N],int   sum,int   current)
     53 {
     54 
     55     int   i;
     56     if(current <=sum)/*检查所有城市*/
     57     {
     58         for(i=1; i <=4; i++) /*测试每种颜色*/
     59         {
     60             r_color[current-1]=i;/*尝试着色*/
     61             if(check(metro,r_color,current))/*若尝试成功*/
     62             {
     63                 drawColour(metro,r_color,sum,current+1);/*检查下一个城市*/
     64                 return;
     65             }
     66         }
     67     }
     68 }
     69 
     70 void   main()
     71 {
     72     /*输入城市的信息*/
     73     int timeCount;
     74     for(timeCount=0; timeCount<N; timeCount++)
     75     {
     76 
     77         inputInfo(city,timeCount);
     78     }
     79 
     80     /*信息传递给邻接矩阵*/
     81     int   r_color[N]= {0};
     82     int   metro[N][N];
     83     int    matrixVerticalCount=0,matrixHorizonCount=0;
     84     for(matrixVerticalCount=0; matrixVerticalCount<N; matrixVerticalCount++)
     85     {
     86         for(matrixHorizonCount=0; matrixHorizonCount<N; matrixHorizonCount++)
     87         {
     88             metro[matrixVerticalCount][matrixHorizonCount]=city[matrixVerticalCount].neighbour[matrixHorizonCount];
     89         }
     90     }
     91 
     92     int printmatrixOne=0,printmatrixTwo;
     93     for(printmatrixOne=0; printmatrixOne<N; printmatrixOne++)
     94     {
     95         for(printmatrixTwo=0; printmatrixTwo<N; printmatrixTwo++)
     96         {
     97             printf("%d ",metro[printmatrixOne][printmatrixTwo]);
     98         }
     99         printf("\n");
    100     }
    101 
    102     drawColour(metro,r_color,5,1);
    103     printf( "\n ");
    104 
    105 
    106     int   i;
    107     for(i=0; i <=4; i++) /*输出着色方案*/
    108         printf( "第%d个城市%s颜色为%3d \n",i+1,city[i].name,r_color[i]);
    109 }

       
           这样基本上将染色算法和信息输入搞定。

           数据结构课设 原文内容要求:设计有效的逻辑数据结构与存储结构表示中国各行政区域的有关信息(如省会城市名,电话区号,人口数,地理位置等)及行政区域间的相邻关系、省会城市间的距离;分析与设计有效的算法对行政区域图进行染色,使每个行政区域染一种颜色且相邻的省份染不同颜色,而总的颜色数最少;另外如在全国省城之间建立通信网,构造费用最低的通信线路铺设方案。

  • 相关阅读:
    centos8上安装中文字符集
    python使用p12个人证书发送S/MIME加密,签名邮件
    linux 下如何获取 cpu 温度
    [官网]Apache Log4j2 最新版安全提示 2.17.0
    电子书下载:Beginning Windows Phone 7 Development
    沙发的含义
    电子书下载:Windows Phone 7 Developer Guide: Building connected mobile applications with Microsoft Silverlight
    电子书下载:The Rails 3 Way, 2nd Edition
    Enfocus Pitstop 7.52 汉化破解版安装
    电子书下载:Professional Flash Mobile Development: Creating Android and iPhone Applications
  • 原文地址:https://www.cnblogs.com/ligen/p/2610497.html
Copyright © 2020-2023  润新知