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 ...
随机推荐
- 深入hibernate的三种状态(转)
hibernate的三种状态: 瞬时对象,持久化对象,托管对象. hibernate的两级缓存:1>一级缓存:session 2>二级缓存:sessionfactory. 瞬时对象: ...
- 计算概论(A)/基础编程练习2(8题)/1:求平均年龄
#include<stdio.h> int main() { // 声明与初始化 , s=, age=; // 输入学生人数 scanf("%d", &n); ...
- java service wrapper日志参数设置及优化
一般在容器比如tomcat/weblogic中运行时,我们都是通过log4j控制日志输出的,因为我们现在很多服务端使用java service wrapper(至于为什么使用jsw,原先是比较排斥使用 ...
- PHP 验证码:扭曲+粘连+变形
一,绪论 由于项目需要,需要加强目前的验证码,我们参照的对象是支付宝. 基于PHP CodeIgniter 框架,代码放置在下面的路径下. /application/libraries 二,主要代码 ...
- ~/.bashrc文件写错, 导致Linux全部命令丢失
问题 今天写bashrc文件的时候, 不小心把PATH结尾带错了,当时不知道,直接就source了, 后来出来的时候发现命令全部提示找不到了... 解决 重新赋予环境变量PATH就行 export P ...
- JAVA I/O(五)多线程网络Socket和ServerSocket
上篇文章介绍了Socket和ServerSocket简单使用和源码,服务器端会对每个客户端请求创建一个线程,为使服务器端能同时处理多个客户端请求,可以采用多线程的方式.本文仅对实例进行简单记录,如下. ...
- 在EditText里输入小写字母时,将小写字母转化为大写显示
1.新建类继承ReplacementTransformationMethod 方法 public class test extends ReplacementTransformationMethod ...
- Makefile使用总结【转】
1. Makefile 简介 Makefile 是和 make 命令一起配合使用的. 很多大型项目的编译都是通过 Makefile 来组织的, 如果没有 Makefile, 那很多项目中各种库和代码之 ...
- c#之有参和无参构造函数,扩展方法
例如在程序中创建 Parent类和Test类,在Test有三个构造函数,parent类继承Test类,那么我们可以在Test类自身中添加 扩展 方法吗? 答案:是不可以的.因为扩展方法必须是静态的,且 ...
- [JavaScript] - form表单转json的插件
jquery.serializejson.js 之前好像记录过,做项目又用到了再记下 在页面中引入js后就可以使用了 示例: //点击设置微信信息的form表单提交按钮后,执行wxConfig的con ...