typedef struct node{ //边结点
int dest; //目标顶点下表
struct node * link; //边链指针
}EdgeNode;typedef struct{ //顶点表
int data; //顶点数据域
EdgeNode * firstAdj;
}VertexNode;typedef struct{ //邻结表
VertexNode VexList [15];
int n, e;
int count[10];
}AdjGraph;AdjGraph G;
//用于判断当前节点上涂上这个颜色可不可行,
//与其邻接节点的颜色做判断,这里用邻接表来存储图的信息
bool isok(int step)
{
EdgeNode *p = G.VexList[i].firstAdj;
int w = 0;
for(; p; p = p->link)
{
w = p->dest;
if(Color[step] == Color[w])
return false;
}
return true;
}
//step表示0-n的国家编号,// color_num是指给color_num的颜色的个数可用
//判断如果给color_num的颜色的个数是否可行,如果可行返回true,否则false
bool DFS(int step)
{
//n是国家个数
if(step >= n)
return true;
else
{
int i;
for(i = 1; i<= color_num; i++)
{
Color[step] = i;
if( isok(step) )
if( DFS(step + 1) ) //继续递归尝试下一个国家
return true;
Color[step] = 0;
}
}
return false;
}
地图着色问题
最新推荐文章于 2021-12-05 21:14:45 发布