Channel Allocation
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 20000/10000K (Java/Other)
Total Submission(s) : 18 Accepted Submission(s) : 7
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.
题意:着色问题, 最多只是用4种颜色而已可以从1开始枚举,然后进行DFS,找出所需要的最小的颜色种数;
思路:对所有的位置进行涂色,进行DFS
AC代码:
1 #include <iostream> 2 #include<cstdio> 3 #include <cstring> 4 using namespace std; 5 int kiss[30][30]={0}; 6 bool kisscode[30][30]={0}; 7 int n; 8 int sgin=0; 9 string s; 10 11 12 int strbian()//对相邻关系进行识别 13 { 14 if(s.length()==2) 15 return 0; 16 char a; 17 a=s[0]; 18 for(int i=2;i<s.length();i++){ 19 kisscode[a-'A'][s[i]-'A']=true; 20 } 21 return 0; 22 } 23 bool dfs(int colersum) 24 { 25 bool coler[6]; 26 memset(coler,true,sizeof(coler)); 27 if(sgin==n+1) 28 return true; 29 for(int i=0;i<n;i++){ 30 if(kisscode[sgin][i]) 31 coler[kiss[sgin][i]]=false; 32 } 33 for(int i=1;i<=colersum;i++){ 34 if(coler[i]){//找到相邻位置上没有的颜色 35 for(int j=0;j<=n;j++){//将第sgin个位置进行涂色 36 kiss[j][sgin]=i; 37 } 38 sgin++; 39 bool a=dfs(colersum); 40 if(a) 41 return true; 42 // sgin--; 43 // for(int j=0;j<=n;j++){ 44 // kiss[j][sgin]=0; 45 // } 46 } 47 } 48 return false; 49 } 50 51 52 53 int main() 54 { 55 // freopen("input.txt","r",stdin); 56 while(cin>>n){ 57 if(n==0) 58 break; 59 memset(kisscode,false,sizeof(kisscode)); 60 memset(kiss,0,sizeof(kiss)); 61 sgin=0; 62 for(int i=0;i<n;i++){ 63 cin>>s; 64 strbian(); 65 } 66 for(int i=1;i<=4;i++){ 67 bool a=dfs(i); 68 if(a){ 69 if(i==1){//进行输出,一定注意1种和其他的区别 70 cout<<i<<" channel needed. "<<endl; 71 break; 72 } 73 else{ 74 cout<<i<<" channels needed. "<<endl; 75 break; 76 } 77 } 78 } 79 } 80 return 0; 81 }