题意: 在一个地图上相邻的点不能用同一个电台,问最少一共需要多少个电台。
思路:用邻接矩阵存点相邻的情况,从一个数量开始枚举能否满足,不足就加一个数量,知道得到最少满足题意电台的数量。
//#include<bits/stdc++.h>//poj不能这个头文件
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int n,M[26][26],color[26],ans,flag;
bool ok(int x,int c)
{
for(int i=0;i<n;i++)
{
if(M[x][i]&&c==color[i])
return false;
}
return true;
}
void dfs(int cur,int total)
{
if(flag==1) return;
if(cur>=n)
{
flag=1;
return;
}
for(int i=1;i<=total;i++)
{
if(ok(cur,i))
{
color[cur]=i;
dfs(cur+1,total);
color[cur]=0;
}
}
if(!flag)
{
ans++;
dfs(cur,total+1);
}
}
int main()
{
while(cin>>n&&n)
{
memset(color,0,sizeof(color));
memset(M,0,sizeof(M));
getchar();
for(int i=0;i<n;i++)
{
char s[50];
gets(s);
for(int j=2;s[j]!='\0';j++)
M[s[0]-'A'][s[j]-'A']=1;
}
flag=0;
ans=1;
dfs(0,1);
if(ans==1)
cout<<"1 channel needed."<<endl;
else
cout<<ans<<" channels needed."<<endl;
}
return 0;
}