四色定理。。zoj 1084 channel allocation

用一种颜色,两种颜色,三种颜色去对图染色,染色成功则输出颜色数,否则输出4

View Code
 1 #include<stdio.h>
2 #include<string.h>
3 bool g[26][26];
4 int used[26];
5 int n;
6 bool dfs(int id,int color)
7 {
8 int i,j;
9 bool flag;
10 for(i=0;i<color;i++)
11 {
12 flag=true;
13 used[id]=i;
14 for(j=0;j<id;j++)
15 if(g[id][j]&&used[j]==used[id])
16 {
17 flag=false;
18 break;
19 }
20 if(flag&&(id==n-1||dfs(id+1,color)))
21 return true;
22 }
23 return false;
24 }
25 int main()
26 {
27 int i,j;
28 bool one;
29 char s[50];
30 while(scanf("%d",&n),n)
31 {
32 memset(g,0,sizeof(g));
33 memset(used,0,sizeof(used));
34 one=true;
35 for(i=0;i<n;i++)
36 {
37 scanf("%s",s);
38 for(j=2;s[j];j++,one=false)
39 {
40 g[i][s[j]-'A']=true;
41 g[s[j]-'A'][i]=true;
42 }
43 }
44 if(one) printf("1 channel needed.\n");
45 else if(dfs(1,2))
46 printf("2 channels needed.\n");
47 else if(dfs(1,3)) printf("3 channels needed.\n");
48 else printf("4 channels needed.\n");
49 }
50 return 0;
51 }


 

posted @ 2011-11-01 23:33  Because Of You  Views(369)  Comments(0Edit  收藏  举报