A:
The repeaters are listed in alphabetical order.
Note that the adjacency is a symmetric relationship; if A is adjacent to B, then B is necessarily adjacent to A. Also, since the repeaters lie in a plane, the graph formed by connecting adjacent repeaters does not have any line segments that cross.
Output
For each map (except the final one with no repeaters), print a line containing the minumum number of channels needed so that no adjacent channels interfere. The sample output shows the format of this line. Take care that channels is in the singular form when only one channel is required.
Sample Input
2
A:
B:
4
A:BC
B:ACD
C:ABD
D:BC
4
A:BCD
B:ACD
C:ABD
D:ABC
0
Sample Output
1 channel needed.
3 channels needed.
4 channels needed.
这道题比赛的时候没看懂,后来问了学长,知道是4色问题,就是一张地图,分为很多块,去不同颜色涂,使得相邻区域的颜色不相同,最多只需要4种不同的颜色
那么这题就是给一张无向图,给点涂色,相邻点不能同颜色,问要多少种,如果图没有边,那就只需要1种,有边的话,就从2种颜色到4种颜色去涂点,这个操作就需要
用dfs去实现,就是给每个点从1到n标记,n是从2到4的,代表不同颜色,若不能符合情况,就中断,令需要的颜色种类加一,继续跑dfs,一直跑,若能够跑完全图,就是
涂完每个点,就是代表只需要这些数量的颜色就可以了,这里有个小技巧,就是如果3种颜色都不够,那就肯定是4种颜色了,就不用跑4种颜色的情况了。
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
int n,ma[27][27],color[27];
int dfs(int id,int color1)
{
int i,j,flag;
for(i=1;i<=color1;i++)
{
color[id]=i;
flag = 1;
for(j=1;j<id;j++)
{
if(ma[j][id]&&color[j]==color[id])
{
flag = 0;
break;
}
}
if(flag && (id == n || dfs(id+1,color1)))
return 1;
}
return 0;
}
int main()
{
char s[30];
int i,j,ans;
while(scanf("%d",&n)!=EOF&&n)
{
ans=1;
memset(ma,0,sizeof(ma));
for(i=1;i<=n;i++)
{
cin>>s;
for(j=2;s[j]!='\0';j++)
{
ma[i][s[j]-'A'+1] = ma[s[j]-'A'+1][i] = 1;
ans=0;
}
}
color[1]=1;
if(ans==1) cout<<"1 channel needed."<<endl;
else if(dfs(2,2)) cout<<"2 channels needed."<<endl;
else if(dfs(2,3)) cout<<"3 channels needed."<<endl;
else cout<<"4 channels needed."<<endl;;
}
return 0;
}