地图着色c语言源代码

#include <stdio.h>
#include <stdlib.h>
#define INFINITY 255   //表示图中两个顶点无关联
typedef struct{
    int vertexNum; //图的顶点数
    int arcNum;    //图的边数
    int *arc;      //指向图中顶点的关联关系数组
    int *color;    //指向各顶点着色情况数组
}Graph;
void creatGraph(Graph *g);
int ColorGraph(Graph *g);
int Conflict(Graph *g,int k);
void dispVertexColor(Graph *g);
int main()
{
    Graph g;   //声明一个Graph类型的图g
    creatGraph(&g);//给图g定义顶点数、边数、生成顶点之间的关联关系
    int m=ColorGraph(&g);
    printf("%d",m);
    dispVertexColor(&g);
    return 0;
}
/**
 * 以下creatGraph()函数
 * 定义图的定点数、边数、
 * 图的邻接矩阵、顶点着色记录数组
 **/
void creatGraph(Graph *g)
{
    int x,y,k,w;
    printf("输入顶点数:\n"); 
    scanf("%d",&g->vertexNum);
    printf("边数:\n"); 
    scanf("%d ",&g->arcNum);
    
    g->arc=(int*)malloc(sizeof(int)*g->vertexNum*g->vertexNum);//创建邻接矩阵
    g->color=(int*)malloc(sizeof(int)*g->vertexNum);//创建顶点着色记录数组
    if(g->vertexNum==1&&g->arcNum==0)
    {
        g->arc[0]=0;
        g->color[0]=1;
        return;
    }
    for(x = 0;x < g->vertexNum;x++){//初始化邻接矩阵中的元素为INFINITY,即顶点之间不连通
        for(y = 0;y < g->vertexNum;y++)
            g->arc[g->vertexNum * x + y]=INFINITY;
        g->color[x]=0;//初始化着色记录数组元素为0,即无色。
    }
    for (k = 0; k < g->arcNum; k++){ //关联矩阵为一个对称矩阵
        
        scanf("%d %d",&x,&y);
        w=1;
        g->arc[g->vertexNum*x+y]=w; //将关联边长度存入图的矩阵x行y列
        g->arc[g->vertexNum*y+x]=w; //将关联边长度存入图的矩阵y行x列
    }
}
void dispVertexColor(Graph *g)       //输出图g中各顶点的着色情况函数
{
    printf("[");
    for(int i = 0;i < g->vertexNum - 1; i++)
        printf("%d,",g->color[i]);
    printf("%d]",g->color[g->vertexNum - 1]);
}
 
int ColorGraph(Graph *g)   //给图的顶点着色,函数返回值为最小着色数的值。
{
  int k=0,m=1;
  if(g->vertexNum==1 && g->arcNum==0)
  {
      g->color[0]=1;
      return m;
  }
  while(1)
  {
  while(k>=0)
  {
      g->color[k]=g->color[k]+1;
      while(g->color[k]<=m)
      {
        if(Conflict(g,k)) break;
        else g->color[k]=g->color[k]+1;
      } 
      if(g->color[k]<=m && k==g->vertexNum-1)
        return m;
      if(g->color[k]<=m && k<g->vertexNum-1)
         k=k+1;
      else
          g->color[k--]=0;
  }
  k=0;
  m++;
  }
}
int Conflict(Graph *g,int k)   //检测顶点k与其邻接点着色是否冲突检,返回1为不冲突,返回0为冲突,此函数由ColorGraph()函数调用
{
    for(int i=0;i<k;i++)
     {
        if(g->arc[g->vertexNum*k+i]!=255&&g->color[i]==g->color[k])
            return 0;
    }
    return 1;
}
 

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个基于C语言和EasyX图形库的可视化地图着色代码,可以直观地展示地图着色的过程。 ```c #include <graphics.h> #include <conio.h> #include <stdio.h> #include <stdlib.h> #include <time.h> #define MAX_VERTEX_NUM 100 // 最大顶点数 #define MAX_COLOR_NUM 10 // 最大颜色数 struct Graph { int vertex[MAX_VERTEX_NUM][2]; // 顶点坐标 int edge[MAX_VERTEX_NUM][MAX_VERTEX_NUM]; // 邻接矩阵 int vertex_num; // 顶点数 }; struct Vertex { int id; // 顶点编号 int x, y; // 顶点坐标 int color; // 顶点颜色 }; Graph map; // 地图 Vertex vertex[MAX_VERTEX_NUM]; // 顶点 int color_num = 0; // 颜色数 int color[MAX_COLOR_NUM]; // 颜色 // 初始化地图 void init_map() { map.vertex_num = 5; map.vertex[0][0] = 100; map.vertex[0][1] = 100; map.vertex[1][0] = 300; map.vertex[1][1] = 100; map.vertex[2][0] = 300; map.vertex[2][1] = 200; map.vertex[3][0] = 200; map.vertex[3][1] = 300; map.vertex[4][0] = 100; map.vertex[4][1] = 200; for (int i = 0; i < map.vertex_num; i++) { for (int j = 0; j < map.vertex_num; j++) { map.edge[i][j] = 0; } } map.edge[0][1] = map.edge[1][0] = 1; map.edge[1][2] = map.edge[2][1] = 1; map.edge[2][3] = map.edge[3][2] = 1; map.edge[3][4] = map.edge[4][3] = 1; map.edge[4][0] = map.edge[0][4] = 1; } // 初始化顶点 void init_vertex() { for (int i = 0; i < map.vertex_num; i++) { vertex[i].id = i; vertex[i].x = map.vertex[i][0]; vertex[i].y = map.vertex[i][1]; vertex[i].color = 0; } } // 初始化颜色 void init_color() { color_num = 4; color[0] = RGB(255, 0, 0); // 红色 color[1] = RGB(0, 255, 0); // 绿色 color[2] = RGB(0, 0, 255); // 蓝色 color[3] = RGB(255, 255, 0); // 黄色 } // 绘制地图 void draw_map() { setbkcolor(WHITE); cleardevice(); setlinestyle(PS_SOLID, 2); setlinecolor(BLACK); for (int i = 0; i < map.vertex_num; i++) { for (int j = i + 1; j < map.vertex_num; j++) { if (map.edge[i][j]) { line(map.vertex[i][0], map.vertex[i][1], map.vertex[j][0], map.vertex[j][1]); } } } for (int i = 0; i < map.vertex_num; i++) { setfillcolor(vertex[i].color); fillcircle(vertex[i].x, vertex[i].y, 20); settextcolor(BLACK); char str[10]; sprintf_s(str, "%d", i); outtextxy(vertex[i].x - 5, vertex[i].y - 7, str); } } // 判断顶点是否可以染色 bool can_color(int v, int c) { for (int i = 0; i < map.vertex_num; i++) { if (map.edge[v][i] && vertex[i].color == c) { return false; } } return true; } // 对顶点进行染色 bool color_vertex(int v) { if (v == map.vertex_num) { // 所有顶点都染色了 return true; } for (int c = 0; c < color_num; c++) { // 枚举颜色 if (can_color(v, c)) { // 如果可以染色 vertex[v].color = color[c]; draw_map(); // 绘制地图 delay(500); if (color_vertex(v + 1)) { // 继续染色下一个顶点 return true; } vertex[v].color = 0; // 回溯 } } return false; } int main() { init_map(); init_vertex(); init_color(); initgraph(400, 400); color_vertex(0); getch(); closegraph(); return 0; } ``` 该程序使用了图形库EasyX来绘制地图和顶点,并使用了延迟函数delay来控制绘制速度。在程序运行时,可以看到地图逐渐被染色,直到所有顶点都被染色为止。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值