Problem Description
When a radio station is broadcasting over a very large area, repeaters are used to retransmit the signal so that every receiver has a strong signal. However, the channels used by each repeater must be carefully chosen so that nearby repeaters do not interfere with one another. This condition is satisfied if adjacent repeaters use different channels.
Since the radio frequency spectrum is a precious resource, the number of channels required by a given network of repeaters should be minimised. You have to write a program that reads in a description of a repeater network and determines the minimum number of channels required.
Since the radio frequency spectrum is a precious resource, the number of channels required by a given network of repeaters should be minimised. You have to write a program that reads in a description of a repeater network and determines the minimum number of channels required.
Input
The input consists of a number of maps of repeater networks. Each map begins with a line containing the number of repeaters. This is between 1 and 26, and the repeaters are referred to by consecutive upper-case letters of the alphabet starting with A. For example, ten repeaters would have the names A,B,C,...,I and J. A network with zero repeaters indicates the end of input.
Following the number of repeaters is a list of adjacency relationships. Each line has the form:
A:BCDH
which indicates that the repeaters B, C, D and H are adjacent to the repeater A. The first line describes those adjacent to repeater A, the second those adjacent to B, and so on for all of the repeaters. If a repeater is not adjacent to any other, its line has the form
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.
Following the number of repeaters is a list of adjacency relationships. Each line has the form:
A:BCDH
which indicates that the repeaters B, C, D and H are adjacent to the repeater A. The first line describes those adjacent to repeater A, the second those adjacent to B, and so on for all of the repeaters. If a repeater is not adjacent to any other, its line has the form
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.
***************************************************************************************************************************
当一个广播电台在一个非常大的地区,广播站会用中继器来转播信号以使得每一个接收器都能接收到一个强烈的信号。然而,每个中继器必须慎重选择使用,使相邻的中继器不互相干扰。如果相邻的中继器使用不同的频道,那么就不会相互干扰。
View Code
由于无线电频道是一有限的,一个给定的网络所需的中继频道数目应减至最低。编写一个程序,读取一个中继网络,然后求出需要的最低的不同频道数。
***************************************************************************************************************************
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 int color[30],map[30][30]; 6 int flag,n; 7 int check(int a,int b) 8 { 9 int i; 10 for(i=0;i<n;i++) 11 { 12 if(map[i][a]&&color[i]==b) 13 return 0; 14 } 15 return 1; 16 } 17 void dfs(int pos,int num) 18 { 19 int i; 20 if(flag==1) return ; 21 if(pos==n) 22 { 23 printf("%d channels needed. ",num-1); 24 flag=1; 25 return ; 26 } 27 if(flag==1) return ; 28 for(i=1;i<num;i++) 29 { 30 if(check(pos,i)) 31 { 32 color[pos]=i; 33 dfs(pos+1,num); 34 } 35 } 36 color[pos]=num; 37 dfs(pos+1,num+1); 38 } 39 int main() 40 { 41 int i,j; 42 char str[30]; 43 while(scanf("%d",&n),n) 44 { 45 flag=0; 46 for(i=0;i<n;i++) 47 { 48 for(j=0;j<n;j++) 49 map[i][j]=0; 50 } 51 for(j=0;j<n;j++) 52 { 53 scanf("%s",str); 54 int len=strlen(str); 55 int a=str[0]-'A'; 56 for(i=2;i<len;i++) 57 { 58 int b=str[i]-'A'; 59 map[a][b]=map[b][a]=1; 60 } 61 } 62 dfs(0,1); 63 } 64 return 0; 65 }