poj1129Channel Allocation

http://poj.org/problem?id=1129

四色定理  最多有四色 从1到四搜

View Code
 1 #include <iostream>

 2 #include<cstdio>

 3 #include<cstring>

 4 #include<stdlib.h>

 5 using namespace std;

 6 int n,w[100][100],co[100],mi,flag;

 7 void dfs(int x,int v)

 8 {

 9     int i,j,f,k=v;

10     if(flag)

11     return ;

12     if(x>n)

13     {

14         mi = v;

15         flag = 1;

16         return ;

17     }

18     else

19     {

20         for(i = 1; i <= 4 ; i++)

21         {

22             f = 1;

23             for(j = 1; j <= n ; j++)

24                 if(w[x][j]&&co[j]==i)

25                 {

26                     f =0 ;

27                     break;

28                 }

29             if(f)

30             {

31                 co[x] = i;

32                 if(k>i)

33                    dfs(x+1,k);

34                 else

35                    dfs(x+1,i);

36                 co[x] = 0;

37             }

38         }

39     }

40 }

41 int main()

42 {

43     int i,j,k,c,m;

44     char s[50];

45     while(cin>>n)

46     {

47         memset(w,0,sizeof(w));

48         flag = 0;

49         if(!n) break;

50         m = n;

51         while(m--)

52         {

53             cin>>s;

54             k = strlen(s);

55             c = s[0]+1-'A';

56             for(i = 2 ; i < k ; i++)

57             {

58                 j = s[i]+1-'A';

59                 w[c][j] = 1;

60                 w[j][c] = 1;

61             }

62         }

63         dfs(1,1);

64         if(mi==1)

65         printf("1 channel needed.\n");

66         else

67         printf("%d channels needed.\n",mi);

68     }

69     return 0;

70 }

 

你可能感兴趣的:(location)