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.

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.

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. 题目大意:一张图中,相邻两点不能涂同一种颜色,要把整张图都涂上,最少需要多少种颜色。
题目解析:数据量不大,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)的更多相关文章

  1. POJ 1129 Channel Allocation(DFS)

    Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13173   Accepted: 67 ...

  2. Channel Allocation(DFS)

    Channel Allocation Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 20000/10000K (Java/Other) ...

  3. 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 ...

  4. POJ 1129 Channel Allocation 四色定理dfs

    题目: http://poj.org/problem?id=1129 开始没读懂题,看discuss的做法,都是循环枚举的,很麻烦.然后我就决定dfs,调试了半天终于0ms A了. #include ...

  5. 迭代加深搜索 POJ 1129 Channel Allocation

    POJ 1129 Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14191   Acc ...

  6. POJ 1129 Channel Allocation DFS 回溯

    Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 15546   Accepted: 78 ...

  7. POJ 3009-Curling 2.0(DFS)

    Curling 2.0 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12158   Accepted: 5125 Desc ...

  8. 题解报告:poj 1321 棋盘问题(dfs)

    Description 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子 ...

  9. 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 ...

随机推荐

  1. 使用Astah画UML类图经验总结

    从学习需求工程与UML开始,就开始接触到Astah这款软件,但是当时完全是为了对UML各种图的了解加深才使用了这款软件.当时画图,都是完全凭借自己想,并没有考虑实际情况,而且画的图都是很简单的,甚至有 ...

  2. Centos 更改系统时间

    .date //查看本地 .hwclock --show //查看硬件的时间 .如果硬件的时间是对不上,那就对硬件的时间进行修改 .hwclock --set --date '2222-22-22 2 ...

  3. jackson 常用注解,比如忽略某些属性,驼峰和下划线互转

    一般情况下使用JSON只使用了java对象与字符串的转换,但是,开发APP时候,我们经常使用实体类来做转换:这样,就需要用到注解: Jackson默认是针对get方法来生成JSON字符串的,可以使用注 ...

  4. C++中两个类中互相包含对方对象的指针问题(转载)

    出处:http://www.cnblogs.com/hanxi/archive/2012/07/25/2608068.html // A.h #include "B.h" clas ...

  5. C语言: 两个int变量相除,结果保留两位小数

    #include<stdio.h> void main() { ,j=; float h; h=(*/)/; printf("%.2f",h); } 注:%f:不指定宽 ...

  6. 论文笔记——Rethinking the Inception Architecture for Computer Vision

    1. 论文思想 factorized convolutions and aggressive regularization. 本文给出了一些网络设计的技巧. 2. 结果 用5G的计算量和25M的参数. ...

  7. C#学习笔记(十四):多态、虚方法和抽象类

    虚方法/非虚方法 < 实例方法 = 非静态方法 = 非类方法(非实例方法 = 静态方法 = 类方法) 函数签名(参数列表,或参数列表 + 返回类型) using System; using Sy ...

  8. IIS中添加ftp站点

    1.创建Windows账号 右击点击“我的电脑”,选择“管理”打开服务器管理的控制台.展开“服务器管理器”,一路展开“配置”.“本地用户和组”,点“用户”项.然后在右边空白处点右键,选择“新用户”将打 ...

  9. HashMap和LinkedHashMap的比较使用

      由于现在项目中用到了LinkedHashMap,并不是太熟悉就到网上搜了一下. import java.util.HashMap; import java.util.Iterator; impor ...

  10. .Net Core集成Office Web Apps(二)

    想要使用OWA需要一台单独的服务器来部署,这对很多人造成困难.而写该文的目的是为了分享有个OWA的集成步骤,它不仅适用于.Net开发环境,其它语言也是一样的,只要实现了需要的服务接口.并且该文不局限与 ...