帮忙罗,这里作业没人会做罗

zealVampire 2003-11-14 09:54:20
1、著名的四色定理指出任何平面区域图均可用四种颜色着色,使相邻区域着不同的颜色。编写程序对给

定的区域图找出所有的不超过四种颜色着色方案。
要求:程序中用1---4表示四种颜色。要着色的n个区域用0---n-1个编号,区域相邻关系用adj[][]矩阵表

示,矩阵i行j列的元素为1 ,表示区域i与区域j 相邻;矩阵i行j列的元素为0,表示区域i 与区域j不相

邻,数组color[i]的值为区域i所着颜色。
adj[][]={{0,1,0,1,1,1,1,1,1,1}
{1,0,1,1,0,1,1,1,1,0}
{0,1,0,1,0,1,1,0,1,1}
{1,1,1,0,1,1,0,0,1,1}
{1,0,0,1,0,1,0,0,0,0}
{1,1,1,1,1,0,1,0,0,1}
{1,1,1,0,0,1,0,0,1,0}
{1,1,0,0,0,0,0,0,1,1}
{1,1,1,1,0,0,1,1,0,1}
{1,0,1,1,0,1,0,1,1,0}}
...全文
24 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
zealVampire 2003-11-22
  • 打赏
  • 举报
回复
2001年的 呵呵
zhao52 2003-11-16
  • 打赏
  • 举报
回复
给你指条路,去看看这几年的高程下午试题,你又有答案了!
hangdian 2003-11-16
  • 打赏
  • 举报
回复
#include〈stdio.h〉

#define N 10

void output(int color[])/*输出一种着色方案*/

{ int i ;

for ( i = 0 ; i < N ; i++ )

printf( "%4d" , color[i] ) ;

printf( "\n" ) ;

}

int back( int *ip ,int color[] ) /*回溯*/

{ int c = 4 ;

while ( c == 4 ){

if ( *ip <= 0 ) return 0 ;

--(*ip) ;

c = color[*ip] ;

color[*ip] = -1 ;

}

return c ;

}

/*检查区域 i ,对 c 种颜色的可用性*/

int color0k( int i , int c , int[][N] , int color[ ] }

{ int j ;

for ( j = 0 ; j < i ; j++ }

if ( adj[i][j] != 0 && color[j] == c )

return 0 ;

return 1 ;

}

 

/*为区域i选一种可着的颜色*/

int select( int i ,int c ,int adj[][N] , int color[ ] )

{ int k ;

for ( k = c ; k <= 4 ; k++ )

if ( colorOK( i,k,adj,color ) )

return k ;

return 0 ;

}

int coloring( int adj[][N] ) /*寻找各种着色方案*/

{ int color[N] , i , c , cnt ;

for ( i = 0 ; i < N ; i++ ) color[i] = -1 ;

i = c = 0 ; cnt = 0 ;

while ( 1 ) {

if ( ( c = select (i,c+l,adj,color) ) == 0 ){

c = back( &i , color) ;

if ( c == 0) return cnt ;

} else {color[i]=c ; i++ ;

if ( i == N ) {

output(color) ;

++cnt ;

c = back( &i , color ) ;

} e1se c = 0 ;

}

}

}

void main()

{ int adj[N][N] =

{ {0,1,0,1,1,1,1,1,1,1},

{1,0,1,1,0,1,1,1,1,0},

{0,1,0,1,0,1,1,0,1,1},

{1,1,1,0,1,1,0,0,1,1},

{1,0,0,1,0,1,0,0,0,0},

{1,1,1,1,1,0,1,0,0,1},

{1,1,1,0,0,1,0,0,1,0},

{1,1,0,0,0,0,0,0,1,1},

{1,1,1,1,0,0,1,1,0,1},

{1,0,1,1,0,1,0,1,1,0}

} ;

printf( "共有%d组解.\n",coloring( adj ) ) ;

}

 


zealVampire 2003-11-15
  • 打赏
  • 举报
回复
好 我自己做做
bitcof 2003-11-15
  • 打赏
  • 举报
回复
你解过骑士旅行和八皇后问题吗,做做会有些收获的。
无非就是几重循环,每种情况试试。
zhouqingyuan 2003-11-14
  • 打赏
  • 举报
回复
这上面有的,自己找找
zealVampire 2003-11-14
  • 打赏
  • 举报
回复
55 代码
lifanxi 2003-11-14
  • 打赏
  • 举报
回复
用回溯,换句话说就是“碰鼻子拐弯”。
依次保证相临格颜色不重复的填,填到不什么都不能填了,就回退一格,换种颜色填,直到全部填满为止。
其实跟八皇后的问题是相似的。

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧