TO 版主:
不好意思, 因为不是很懂请教的方式, 望原谅!
现在, 有这样一个问题:
假设有N个多叉路口, 要用编程语言设计一个交通灯系统;
现在, 本人只用地图着色和相邻矩阵编了一个具体的六路口交通灯的核心算法,如下,
int allcolor[4];/*可用的颜色*/
#define N 21
void color(int metro[N][N],int r_color[N],int sum)
{
int i,j,k;
for(i=1;i<=sum;i++)/*检查所有路口交通情况*/
for(j=1;j<=4;j++)/*对每个路口尝试4种颜色的着色方案*/
{
r_color[i]=j;/*尝试着色*/
for(k=1;k<i;k++)/*检查是否与相邻路口颜色相同*/
if(metro[i][k]==1&&r_color[k]==r_color[i])
break;/*相同则跳出,此时有k<i,则下面条件不成立,继续尝试下一种颜色*/
if(k>=i)/*若不相同,则使用当前颜色,并检查下一个路口*/
break;
}
}
void main()
{
int r_color[N]={0};
int t_color[N]={0};
int i;
int start;/*着色的起点*/
int metro[N][N]={{0},
{0,1,1,1,1,1,1},
{0,1,1,1,1},
{0,1,1,1,0,0,1},
{0,1,1,0,1,1},
{0,1,0,0,1,1,1,0,0,1,0,0,0,0,0,0,1},
{0,1,0,1,0,1,1,1,1,1},
{0,0,0,0,0,0,1,1,1},
{0,0,0,0,0,0,1,1,1,1,0,0,1},
{0,0,0,0,0,1,1,0,1,1,0,0,1,1,1,0,1},
{0,0,0,0,0,0,0,0,0,0,1,1,0,1,0,1,0,0,0,1},
{0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,1},
{0,0,0,0,0,0,0,0,1,1,0,1,1,1,0,0,0,0,0,1,1},
{0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1},
{0,0,0,0,0,0,0,0,0,1,0,0,0,1,1,1,1},
{0,0,0,0,0,0,0,0,0,0,1,0,0,1,1,1,1,1,0,1},
{0,0,0,0,1,0,0,0,1,0,0,0,0,1,1,1,1},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1},
{0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,1,0,0,1,1,1},
{0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,1}};
allcolor[0]=1;
allcolor[1]=2;
allcolor[2]=3;
allcolor[3]=4;/*选色顺序,顺序不同,结果不同*/
start=1;
/* clrscr();*/
printf("\nAll color is:\n");
for(i=0;i<4;i++)/*当前选色顺序*/
printf("%d ",allcolor[i]);
color(metro,t_color,21);
printf("\nAnd the start metro is:%d\n",start);
for(i=1;i<=21;i++)
printf("%3d",t_color[i]);
getch();
}
但是, 若N个路口, 应该怎样构造邻接矩阵?
请版主指教啊!
也请版主发到其他的相关版面!
感激不尽!!!!!