四色问题的搜索。比较简单。
#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
struct Node{
int color;
int adj[25];
int adj_num;
};
Node nodes[26];
int n;
int max_color;
int visit[27];
int main(){
while(cin>>n,n!=0){
for(int i=0;i<n;++i){
nodes[i].adj_num = 0;
nodes[i].color = 0;
}
max_color = 0;
char u,v;
getchar();
for(int i=0;i<n;++i){
getchar();
getchar();
while(v=getchar(),v!='\n'){
nodes[i].adj[ nodes[i].adj_num++ ] = 0+v-'A';
}
}
for(int i=0;i<n;++i){
memset(visit,0,sizeof(visit));
for(int j=0;j<nodes[i].adj_num;++j){
if(nodes[ nodes[i].adj[j] ].color )
visit[nodes[ nodes[i].adj[j] ].color] = 1;
}
for(int k=1;k<=4;++k){
if(!visit[k]){
nodes[i].color = k;
break;
}
}
max_color = max(max_color,nodes[i].color);
}
if(max_color==1)
cout<<"1 channel needed."<<endl;
else
cout<<max_color<<" channels needed."<<endl;
}
return 0;
}