Channel Allocation

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 20000/10000K (Java/Other)
Total Submission(s) : 18   Accepted Submission(s) : 7
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.

 
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.
题意:着色问题, 最多只是用4种颜色而已可以从1开始枚举,然后进行DFS,找出所需要的最小的颜色种数;
思路:对所有的位置进行涂色,进行DFS
AC代码:
 #include <iostream>
#include<cstdio>
#include <cstring>
using namespace std;
int kiss[][]={};
bool kisscode[][]={};
int n;
int sgin=;
string s; int strbian()//对相邻关系进行识别
{
if(s.length()==)
return ;
char a;
a=s[];
for(int i=;i<s.length();i++){
kisscode[a-'A'][s[i]-'A']=true;
}
return ;
}
bool dfs(int colersum)
{
bool coler[];
memset(coler,true,sizeof(coler));
if(sgin==n+)
return true;
for(int i=;i<n;i++){
if(kisscode[sgin][i])
coler[kiss[sgin][i]]=false;
}
for(int i=;i<=colersum;i++){
if(coler[i]){//找到相邻位置上没有的颜色
for(int j=;j<=n;j++){//将第sgin个位置进行涂色
kiss[j][sgin]=i;
}
sgin++;
bool a=dfs(colersum);
if(a)
return true;
// sgin--;
// for(int j=0;j<=n;j++){
// kiss[j][sgin]=0;
// }
}
}
return false;
} int main()
{
// freopen("input.txt","r",stdin);
while(cin>>n){
if(n==)
break;
memset(kisscode,false,sizeof(kisscode));
memset(kiss,,sizeof(kiss));
sgin=;
for(int i=;i<n;i++){
cin>>s;
strbian();
}
for(int i=;i<=;i++){
bool a=dfs(i);
if(a){
if(i==){//进行输出,一定注意1种和其他的区别
cout<<i<<" channel needed. "<<endl;
break;
}
else{
cout<<i<<" channels needed. "<<endl;
break;
}
}
}
}
return ;
}

Channel Allocation(DFS)的更多相关文章

  1. POJ 1129 Channel Allocation(DFS)

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

  2. POJ-1129 Channel Allocation (DFS)

    Description When a radio station is broadcasting over a very large area, repeaters are used to retra ...

  3. LeetCode Subsets II (DFS)

    题意: 给一个集合,有n个可能相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: 看这个就差不多了.LEETCODE SUBSETS (DFS) class Solution { publ ...

  4. LeetCode Subsets (DFS)

    题意: 给一个集合,有n个互不相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: DFS方法:由于集合中的元素是不可能出现相同的,所以不用解决相同的元素而导致重复统计. class Sol ...

  5. HDU 2553 N皇后问题(dfs)

    N皇后问题 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description 在 ...

  6. 深搜(DFS)广搜(BFS)详解

    图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...

  7. 【算法导论】图的深度优先搜索遍历(DFS)

    关于图的存储在上一篇文章中已经讲述,在这里不在赘述.下面我们介绍图的深度优先搜索遍历(DFS). 深度优先搜索遍历实在访问了顶点vi后,访问vi的一个邻接点vj:访问vj之后,又访问vj的一个邻接点, ...

  8. 深度优先搜索(DFS)与广度优先搜索(BFS)的Java实现

    1.基础部分 在图中实现最基本的操作之一就是搜索从一个指定顶点可以到达哪些顶点,比如从武汉出发的高铁可以到达哪些城市,一些城市可以直达,一些城市不能直达.现在有一份全国高铁模拟图,要从某个城市(顶点) ...

  9. 深度优先搜索(DFS)和广度优先搜索(BFS)

    深度优先搜索(DFS) 广度优先搜索(BFS) 1.介绍 广度优先搜索(BFS)是图的另一种遍历方式,与DFS相对,是以广度优先进行搜索.简言之就是先访问图的顶点,然后广度优先访问其邻接点,然后再依次 ...

随机推荐

  1. 《C++反汇编与逆向分析技术揭秘》——函数的工作原理

    各种调用方式的考察 示例: cdecl方式是调用者清空堆栈: 如果执行的是fastcall: 借助两个寄存器传递参数: 参数1和2借助局部变量来存储: 返回值 如果返回值是结构体: 返回值存放在eax ...

  2. excel 导入 与 导出

    Excel导入 public ActionResult Excel(HttpPostedFileBase file)        {            HttpPostedFileBase fi ...

  3. 详细介绍Java垃圾回收机制

    垃圾收集GC(Garbage Collection)是Java语言的核心技术之一,之前我们曾专门探讨过Java 7新增的垃圾回收器G1的新特性,但在JVM的内部运行机制上看,Java的垃圾回收原理与机 ...

  4. Highcharts属性介绍

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  5. [河南省ACM省赛-第三届] 网络的可靠性 (nyoj 170)

    题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=170 根据题意,需要找到度数为1的结点个数,如下图: #include<iostre ...

  6. 利用LibreOffice与ImageMagick将网页分享至微信

    现在越来越多的内容分享都是在微信上进行了.然而,若想将电脑浏览器中看到的感兴趣的网页分享至微信,则只能以纯文本的方式粘贴超级链接,而不能直接拷贝图文混排的HTML.因此,我想到不妨借助LibreOff ...

  7. BGP多线单IP技术实现形式以及其他双线对比

    自从电信与网通分离之后,北方网通与南方电信网络的互联瓶颈问题一直没有得到很好的解决,这个问题也严重困扰广大的ICP服务商.ICP也只能根据自己网站主流用户群是在南方还是在北方,服务重点是在南方还是北方 ...

  8. rebase

    /BASE (Base Address) https://msdn.microsoft.com/en-us/library/f7f5138s.aspx Need for Rebasing a DLL( ...

  9. freemarker + spring mvc + spring + mybatis + mysql + maven项目搭建

    今天说说搭建项目,使用freemarker + spring mvc + spring + mybatis + mysql + maven搭建web项目. 先假设您已经配置好eclipse的maven ...

  10. JAVA语法基础(课堂ppt问题总结)

    一:运行源代码EnumTest.java,分析运行结果. 代码如下: public class EnumTest { public static void main(String[] args) { ...