main.c
#include"map.h"
//#include"stack.h"
#include
//一个邻接矩阵
int main()
{
int adjoin[NUM][NUM];
//InitMap(adjoin);
SeqStack stack;
//InitSeqStack(&stack);
Map(adjoin,&stack);
//最后再打印出来
for(int i=0;i
map.c
#include
#include"map.h"
//#include"stack.h"
void InitMap(int adjoin[NUM][NUM])
{
int temp[NUM][NUM]=
{
{0,1,1,1,1,1,0},
{1,0,0,0,0,1,0},
{1,0,0,1,1,0,0},
{1,0,1,0,1,1,0},
{1,0,1,1,0,1,0},
{1,1,0,1,1,0,0},
{0,0,0,0,0,0,0}
};
int i=0;
for(;isize-1;i++)//stack->size-1为stack-》color的最大下标
{
if(adjoin[sub][i]==1&&sub!=i)//adjoin[sub][i]==1即与编号为i的块相邻
{
if(stack->color[i]==color_temp)
{
return 1;
}
}
}
return 0;
}
void Map(int adjoin[NUM][NUM],SeqStack *stack)
{
InitMap(adjoin);
InitSeqStack(stack);
SeqStackPush(stack,1);//第一个染一号色
//int i;
//int PopFlag=0;
int ColorRem=0;
while(stack->size!=NUM)//stack->size是栈中元素的个数
{
//if(stack->size)
int color_temp=1;
if(AdjoinSearch(stack,adjoin,stack->size,color_temp)==1||ColorRem>=color_temp)//相邻的有重复的
//stack->size即为要检验的元素在邻接表中的下标
{
color_temp=2;
if(AdjoinSearch(stack,adjoin,stack->size,color_temp)==1||ColorRem>=color_temp)//相邻的有重复的
//stack->size即为要检验的元素在邻接表中的下标
{
color_temp=3;
if(AdjoinSearch(stack,adjoin,stack->size,color_temp)==1||ColorRem>=color_temp)//相邻的有重复的
//stack->size即为要检验的元素在邻接表中的下标
{
color_temp=4;
if(AdjoinSearch(stack,adjoin,stack->size,color_temp)==1||ColorRem>=color_temp)//相邻的有重复的
//stack->size即为要检验的元素在邻接表中的下标
{//说明四种颜色都不行
//SeqStackPop(&stack);
if( (stack->color[stack->size-1]<=3)&&(AdjoinSearch(stack,adjoin,stack->size-1,stack->color[stack->size-1]+1)==0))
{
SeqStackChange(stack);
ColorRem=0;
}
else
{
//PopFlag=1;
ColorRem=stack->color[stack->size-1];
//SeqStackPop(stack);
stack->size--;
}
}
else
{
SeqStackPush(stack,color_temp);
ColorRem=0;
}
}
else
{
SeqStackPush(stack,color_temp);
ColorRem=0;
}
}
else
{
SeqStackPush(stack,color_temp);
ColorRem=0;
}
}
else
{
SeqStackPush(stack,color_temp);
ColorRem=0;
}
}
}
map.h
#define NUM 7
#include"stack.h"
void Map(int adjoin[NUM][NUM],SeqStack *satck);
void InitMap(int adjoin[NUM][NUM]);
int AdjoinSearch(SeqStack *stack,int adjoin[NUM][NUM],int sub,int color_temp);
stack.c
#include"stack.h"
#include
void InitSeqStack(SeqStack *stack)
{
stack->capacity=1000;
stack->size=0;
stack->color=(int *)malloc(sizeof(int)*stack->capacity);
//第一个stack[0]
}//初始化了一片空间,但是里面没有存任何东西,stack->size=0,下标为size-1时为首元素地址
void SeqStackPush(SeqStack *stack,int color)
{
stack->size++;
stack->color[stack->size-1]=color;
}
int SeqstackPop(SeqStack *stack)
{
stack->size--;
if(stack->size==0)
return 0;//无解,出大问题
else
return 1;
}
void SeqStackChange(SeqStack *stack)
{
if(stack->size!=0)
stack->color[stack->size-1]++;//进这个函数的前提就是有色可染,不用多考虑
}
stack.h
typedef struct SeqStack
{
int * color;
int size;
int capacity;
}SeqStack;
void InitSeqStack(SeqStack *stack);
void SeqStackPush(SeqStack *stack,int color);
int SeqStackPop(SeqStack * stack);
void SeqStackChange(SeqStack *stack);//栈顶元素颜色递增