{GIS算法}地图四色着图/C语言代码/算法

12 篇文章 9 订阅
11 篇文章 2 订阅

四色原理是什么?网上原理有很多,不懂可以自己搜。
把需要填图的区域看作泰森多边形,每个区域的顶点存储颜色,点之间关系就是TIN三角形,区域和区域的关系就转化为点和点之间的关系。
把所有点储存在邻接表里,着色采用回溯法,原理就是图的遍历。
不懂可以复制下来自己运行一下。

#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX_VERTEX_NUM 20


typedef struct ArcNode {
	int adjvex; 
	struct ArcNode *nextarc; 
} ArcNode; 
typedef struct {
	float x;
	float y;
} Vertex; 
typedef struct VNode {
	Vertex data; 
	ArcNode *firstarc;
	int color; 
} VNode;
typedef struct{ 
	VNode vertics[MAX_VERTEX_NUM]; 
	int vexnum, arcnum; 
} UDGraph;

bool isColorOK(UDGraph, int);
bool getColorPowset(UDGraph &, int, int); 
int createUDGraph(UDGraph &);
void outputGraph(UDGraph);

bool isColorOK(UDGraph G, int node) 
{ 
	VNode V = G.vertics[node - 1];
	ArcNode *T = V.firstarc;
	while(T) 
	{
		if(V.color == G.vertics[T->adjvex].color)  
			return false;
		T = T->nextarc; 
	}
	return true;
}

bool getColorPowset(UDGraph &G, int color_Num, int step) 
{ 
	if(step > G.vexnum) 
		return true;
	else 
	{
		int i;
		for(i=1; i<=color_Num; i++) 
		{
			G.vertics[step - 1].color = i;
			if(isColorOK(G, step)) 
				if(getColorPowset(G, color_Num, step + 1)) 
					return true;
			G.vertics[step - 1].color = 0;
		}
	}
	return false;
}

int createUDGraph(UDGraph &G) 
{
	int a, b, i;
	ArcNode *p;
	printf("请输入结点的个数和边的个数:");
	scanf("%d,%d", &(G.vexnum), &(G.arcnum));
	getchar(); 
	for(i=0; i<G.vexnum; i++) 
	{
		G.vertics[i].firstarc =NULL;
		G.vertics[i].color = 0;
	}
	printf("请输入这%d个点之间的拓扑关系:\n", G.vexnum);
	for(i=1; i<=G.arcnum; i++) 
	{
		scanf("%d,%d", &a, &b); 
		getchar();
		p = (ArcNode*)malloc(sizeof(ArcNode)); 
		if(!p) exit(-2);
		p->adjvex = b - 1;
		p->nextarc = G.vertics[a - 1].firstarc;
		G.vertics[a - 1].firstarc = p;
		p = (ArcNode*)malloc(sizeof(ArcNode)); 
		if(!p) exit(-2);
		p->adjvex = a - 1;
		p->nextarc = G.vertics[b - 1].firstarc;
		G.vertics[b - 1].firstarc = p;
	}
	return 1;
}
void outputGraph(UDGraph G) 
{
	int i;
	for(i=0; i<G.vexnum; i++) 
		printf("结点%d的着色方案为:%d\n", i+1, G.vertics[i].color);
}
int main() 
{
	UDGraph G;
	createUDGraph(G);
	if(getColorPowset(G, 4, 1)) 
	{
		printf("图形图色成功,为:\n");
		outputGraph(G);
	} 
	else 
		printf("图形图色失败\n");
	return 1;
}

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

撼沧

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值