Channel Allocation
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 20000/10000K (Java/Other)
Total Submission(s) : 13 Accepted Submission(s) : 6
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.<br><br>Following the number of repeaters is a list of adjacency relationships. Each line has the form:<br><br>A:BCDH<br><br>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<br><br>A:<br><br>The repeaters are listed in alphabetical order.<br><br>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. <br>
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.
Source
PKU
题意:
给你从A开始的n个广播站,要求相邻的广播站不能用同一个中继站,输入的是广播站之间的联系,要你输出最小的需要中继站的数目!
思路:
运用深搜,两个方向,一个是广播站数目加一中继站不变,一个是中继站加一而且广播站也加一!用一个二维数组记录彼此广播站之间的联系!注意将字符转换成数字!!!
注意细节,当结果是1时channel没有s!!注意观察。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int v[30],mp[30][30];
int flag,n;
bool judge(int a,int b) //判断
{
int i;
for(i=0;i<n;i++)
{
if(mp[i][a] && v[i]==b)
return 0;
}
return 1;
}
void dfs(int num,int sum)//num是第几个广播台数 sum是需要的中继器个数
{
int i;
if(flag==1)
return;
if(num==n) //结束条件,即广播站数等出现的了
{
cout<<sum-1;
if(sum-1>1)
cout<<" channels needed."<<endl;
else
cout<<" channel needed."<<endl;
flag=1;
return;
}
if(flag==1)
return;
for(i=1;i<sum;i++)
{
if(judge(num,i)) //这里的 i是 假设 的一个中继站
{
v[num]=i;
dfs(num+1,sum);
}
}
v[num]=sum; //新增一个中继站并且赋给广播站num
dfs(num+1,sum+1);
}
int main()
{
int i,j;
char str[30];
while(cin>>n,n)
{
flag=0;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
mp[i][j]=0;
}
for(j=0;j<n;j++)
{
cin>>str;
int len = strlen(str);
int a=str[0]-'A';
for(i=2;i<len;i++)
{
int b=str[i]-'A';
mp[a][b]=mp[b][a]=1;
}
}
dfs(0,1);
}
return 0;
}
心得:
纠结了好久。。。。。。