c语言地图染色程序,求C语言地图四染色代码..

???代码改色没用??

#include

#include

#include

#include

#define INIT_STACK_SIZE 10

#define STACKINREMENT 5

typedef struct{

int * top;

int * base;

int stacksize;

}SqStack;

int checkMap(int , SqStack &, int );

void Push(SqStack &, int);

int Pop(SqStack &);

int MAP[20][20] ;

int locationNum ;

/*

int MAP[7][7] = {{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}};

*/

void PaintMap()

{

void GetMap();

SqStack stack;

stack.base = (int *)malloc(INIT_STACK_SIZE *sizeof(int));

stack.top = stack.base;

stack.stacksize = INIT_STACK_SIZE;

*stack.top++  = 1;

int color = 1;   int section = 2;

int mark = 0;

GetMap() ; /*要求用户输入地图*/

while ( section<=locationNum)

{

while(( color <= 4)&&(section <= locationNum))

{

mark = checkMap(section, stack, color);

// 若不相邻,或若相邻且不重色,对下一个区域判断。

if (mark < section)   color = color + 1; //相邻且重色

else

{

Push(stack, color);  section = section + 1;  color = 1;

}//相邻且不重色

}

if(color > 4)

{

section = section - 1;   color = Pop(stack) + 1;

}

}

printf("染色的结果是  :\n" );

for(int i = 0; i < locationNum ; i++)

printf("%d  ", *(stack.base + i));

printf("\n");

}

/*************************************************

要求用户输入地图的有关信息

*************************************************/

void GetMap(){

printf("需要染色的地区数 : ");

scanf("%d", &locationNum);

while(getchar() != '\n');

printf("输入地图 : \n");

for(int i = 0 ; i < locationNum ; i++)

{

char ch , lastCh ; int num = 0;

printf("与第 %d 号区域相邻的所有区域号 : " , i + 1);

while((ch = getchar()) != '\n')

{

if(ch == ' ' ){

MAP[i][num - 1] = 1 ;

num = 0;

}else

num = num * 10 + (ch - '0') ;

lastCh = ch ;

}

if(lastCh != ' ')

MAP[i][num - 1] = 1 ;

}

for(int j = 0; j < locationNum ; j++)

{

for(int k = 0; k < locationNum ; k++)

if(MAP[j][k] != 1)

MAP[j][k] = 0 ;

}

printf("输入的地图是 : \n");

for(int m = 0; m < locationNum ; m++){

for(int n = 0; n < locationNum ; n++)

printf("%d  ", MAP[m][n]);

printf("\n");

}

}

/******************************************************

判断当前地区能否染当前的颜色

******************************************************/

int checkMap(int index, SqStack &stack, int color){

int mark = 1 ;

while((mark < index )&& (*(stack.base + mark - 1)) * MAP[mark-1][index-1] != color && color <= 4 )

mark++;

return  mark;}

void Push(SqStack &stack, int e)

{

if(stack.top-stack.base >= stack.stacksize){

stack.base = (int *)realloc(stack.base, (stack.stacksize + STACKINREMENT)*sizeof(int));

stack.top = stack.base + stack.stacksize;

stack.stacksize = stack.stacksize + STACKINREMENT;

}

*stack.top++ = e;

}

int Pop(SqStack &stack)

{

int e = 0;

if(stack.top - stack.base <= 0){

printf("ERROR !");

exit(1);

}

e = *--stack.top ;

return e;

}

int main()

{

PaintMap() ;

return 0 ;

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值