openjudge_2.5基本算法之搜索_131:Channel Allocation

题目

131:Channel Allocation
总时间限制: 1000ms 内存限制: 65536kB
描述
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.
输入
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.
输出
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.
样例输入
2
A:
B:
4
A:BC
B:ACD
C:ABD
D:BC
4
A:BCD
B:ACD
C:ABD
D:ABC
0
样例输出
1 channel needed.
3 channels needed.
4 channels needed.

翻译

题目
当一个无线电台在很大的区域上广播时,中继器被用来重新传输信号,这样每个接收器都有很强的信号。
但是,必须仔细选择每个中继器使用的信道,以便附近的中继器不会相互干扰。
如果相邻中继器使用不同的信道,则满足此条件。
由于无线电频谱是一种宝贵的资源,因此应尽量减少给定中继器网络所需的信道数量。
您必须编写一个程序,读取中继器网络的描述,并确定所需的最小信道数。

输入
输入包括许多中继器网络的映射。每个地图以一个数开始,表示中继器数量。
这介于1和26之间,并且中继器由字母表中以A开头的连续大写字母表示。
例如,十个中继器的名称为A、B、C,。。。,I和J。具有零中继器的网络表示输入结束。
在中继器的数量之后是相邻关系的列表。每条线的形式如下:
A:BCDH
这指示中继器B、C、D和H与中继器A相邻adjacent。第一行描述了与中继器A邻近的那些中继器,
第二行描述了所有中继器的与B邻近的中继器,依此类推。如果中继器不与任何其他中继器相邻,
则其线路具有以下形式
A:
中继器按字母顺序排列。
注意,邻接是一种对称关系;如果A与B相邻,那么B必然与A相邻。此外,由于中继器位于一个平面中,
通过连接相邻中继器形成的图不具有任何交叉的线段。

输出
对于每个地图(除了最后一个没有中继器的地图),打印一行包含所需通道的最小数量,
这样就不会有相邻通道干扰。示例输出显示了此行的格式。当只需要一个通道时,
请注意通道是单数形式。

理解

每个单词都有多个意思,翻译过来不定能理解真实题意,还是得需要提高阅读理解能力。
多个塔,有些塔相邻,为避免彼此感染相邻的塔不能共用一个频率。
不相邻的塔可以共用一个频率
遍历每个出发塔,宽搜所相邻的塔,彼此不能相邻塔的频率=出发塔频率+1

代码

#include <bits/stdc++.h>
using namespace std;
int n,//有几组相邻关系图
m[26];//每个信号塔的频率
vector v[26];//每个信号塔的相邻塔
int main(){
//freopen(“data.cpp”,“r”,stdin);
while(cin>>n&&n){
int ans=1;//初始起码得一个频率
memset(m,0,sizeof(m));//初始化
for(int i=0;i<n;i++)v[i].clear();//清空是如此重要
string s;
for(int i=0;i<n;i++){
cin>>s;
m[i]=1;//出发塔的初始频率
for(int j=2;j<s.length();j++){
if(j!=0)v[i].push_back(s[j]-‘A’);//相邻塔放进容器里
}
}
//遍历每个顶点
for(int i=0;i<n;i++){//从0也就是’A’开始
if(m[i]!=1)continue; //出发塔出发过了,
queue q;q.push(i);
while(!q.empty()){//宽搜
int z=q.front();q.pop();
for(int j=0;j<v[z].size();j++){//遍历相邻塔
if(m[z]= =m[v[z][j]]){//如果相邻塔的频率一样,
m[v[z][j]]=m[z]+1;//频率在出发塔的频率基础上+1
q.push(v[z][j]);
ans=max(ans,m[v[z][j]]);
}
}
}
}
if(ans==1)cout<<ans<<" channel needed.“<<endl;
else cout<<ans<<” channels needed."<<endl;
}
return 0;
}

小结

一般宽搜需要标记已经访问过点,现在是标记出发塔。而出发塔的频率不等于1,就标明已经访问过了。
第一个出发塔肯定是’A’,所以肯定是’A’-x=0,不用再计算。

  • 9
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值