POJ-1129 Channel Allocation (DFS)
Description
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
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.
Output
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. 题目大意:一张图中,相邻两点不能涂同一种颜色,要把整张图都涂上,最少需要多少种颜色。
题目解析:数据量不大,DFS即可。根据四色原理,最多只有四种颜色。先将第一个点涂上一种颜色(这是必然的),然后一个点一个点涂下去,当涂到最后一个点时,合计当前方案的颜色种数,然后更新最优解。 代码如下:
# include<iostream>
# include<cstdio>
# include<set>
# include<string>
# include<cstring>
# include<algorithm>
using namespace std;
int n,ans,col[],mp[][];
bool ok(int p,int c)
{
for(int i=;i<n;++i)
if(mp[p][i]&&col[i]==c)
return false;
return true;
}
void dfs(int p)
{
if(p==n-){
for(int k=;k<=;++k){
if(ok(p,k)){
col[p]=k;
set<int>s;
for(int i=;i<n;++i){
s.insert(col[i]);
}
if(ans>s.size())
ans=s.size();
col[p]=;
}
}
return ;
}
for(int i=;i<=;++i){
if(ok(p,i)){
col[p]=i;
dfs(p+);
col[p]=;
}
}
}
int main()
{
while(scanf("%d",&n)&&n)
{
string p;
memset(mp,,sizeof(mp));
for(int i=;i<n;++i){
cin>>p;
for(int j=;j<p.size();++j)
mp[i][p[j]-'A']=mp[p[j]-'A'][i]=;
}
memset(col,,sizeof(col));
ans=;
col[]=;
dfs();
if(ans==){
printf("%d channel needed.\n",ans);
}else
printf("%d channels needed.\n",ans);
}
return ;
}
POJ-1129 Channel Allocation (DFS)的更多相关文章
- POJ 1129 Channel Allocation(DFS)
Channel Allocation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 13173 Accepted: 67 ...
- Channel Allocation(DFS)
Channel Allocation Time Limit : 2000/1000ms (Java/Other) Memory Limit : 20000/10000K (Java/Other) ...
- poj 1129 Channel Allocation(图着色,DFS)
题意: N个中继站,相邻的中继站频道不得相同,问最少需要几个频道. 输入输出: Sample Input 2 A: B: 4 A:BC B:ACD C:ABD D:BC 4 A:BCD B:ACD C ...
- POJ 1129 Channel Allocation 四色定理dfs
题目: http://poj.org/problem?id=1129 开始没读懂题,看discuss的做法,都是循环枚举的,很麻烦.然后我就决定dfs,调试了半天终于0ms A了. #include ...
- 迭代加深搜索 POJ 1129 Channel Allocation
POJ 1129 Channel Allocation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14191 Acc ...
- POJ 1129 Channel Allocation DFS 回溯
Channel Allocation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 15546 Accepted: 78 ...
- POJ 3009-Curling 2.0(DFS)
Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12158 Accepted: 5125 Desc ...
- 题解报告:poj 1321 棋盘问题(dfs)
Description 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子 ...
- POJ 2251 Dungeon Master(dfs)
Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is co ...
随机推荐
- ACM题目————中位数
题目描述 长为L的升序序列S,S[L / 2]为其中位数. 给出两个等长升序序列S1和S2,求两序列合并并排序后的中位数. 输入 多组数据,每组第一行为n,表示两个等长升序序列的长度. 接下来n行为升 ...
- servlet+jsp+java实现Web应用
servlet+jsp+java实现Web应用 环境: 1,eclipse 2,tomcat3,eclipse tomcat 插件 开发过程: 1,建立一个Dynamic Web Project 2, ...
- CSS实现三角形、梯形、平行四边形、圆形、椭圆形、对话框、自适应正方形
本文篇幅较长,希望能坚持看完,转载请注明出处,如果觉得好文请给个赞吧 CSS实现梯形 CSS实现三角形和梯形主要是依靠border是梯形的特性来做的,有点像相框的那种感觉. 首先我们先给一个正方形设置 ...
- jQuery 源码分析:当 selector 传来一个函数时,怎么进行处理?
本文章为 0.9 版本,将会在稍后润色更新.本文使用的 jQuery 版本为 3.4.0 我们知道使用 $ 操作符时,可以往里面塞很多类型的参数,字符串,对象,函数...,jQuery 会根据不同的参 ...
- Python3基础 str split 用指定的字符将字符串分割
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- Android和PHP开发最佳实践
Android和PHP开发最佳实践 <Android和PHP开发最佳实践>基本信息作者: 黄隽实丛书名: 移动应用开发技术丛书出版社:机械工业出版社ISBN:9787111410508上架 ...
- Unity3D学习笔记(八):四元素和书籍推荐
书籍推荐: 3D数学基础:图形与游戏开发——游戏软件开发专家系列(美)邓恩 Unity Shader入门精要 冯乐乐(92年) 数据结构(Python语言描述) 数据结构.算法与应用(C++语言描述) ...
- LaTex: Undefined citation warnings 解决方法
参考 Undefined citations LaTex: Undefined citation warnings 解决方法 在使用TexMaker编译文献的时候,出现引用参考文献的问题: Packa ...
- 05_Flume_timestamp interceptor实践
1.目标场景 2.Flume Agent配置 # specify agent,source,sink,channel a1.sources = r1 a1.sinks = k1 a1.channels ...
- SSH公钥登录且禁止密码登录及更改默认端口
1.ssh生成公私钥 ssh-keygen -t rsa -C "zhangsan@qq.com" 生成密钥的位置如下,id_rsa是私钥.id_rsa.pub是公钥: ➜ .ss ...