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. Chrome开发者控制台的这些功能你都知道吗?

    Chrome内置了一些开发者工具,这些工具提供了很多的功能.今天,我们将会专注于JavaScript控制台. 在我编程的过程中,这个控制台为我提供了大量的帮助. 如果你正在电脑端阅读这篇文章,你可以在 ...

  2. mozilla your firefox profile cannot be loaded. it may be missing or inaccessible

    check the permissions ls -l ~/.cache | grep mozilla fix the permissions sudo chown -R $USER:$USER ~/ ...

  3. 浙大pat1009题解

    1009. Product of Polynomials (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...

  4. IntentService和Service的区别

    整个看下来是一个Service+Thread+handle的结合体, Service:比Activity的被kill的级别低 Thread:不阻塞UI线程 Handle:队列式的消息循环 那这个玩意的 ...

  5. ResultSet.TYPE_SCROLL_SENSITIVE问题(完全摘自他人)

    摘自CSDN博客 我们先来做一个例子,在例子中我用的是mysql-essential-5.1.30-win32版. 来跟我做以下几个命令: mysql> create database axma ...

  6. android 之 java环境部署

    上甲骨文公司官网下载最新的jdk http://www.oracle.com/technetwork/cn/java/javase/downloads/jdk8-downloads-2133151-z ...

  7. CUDA编程入门,Dim3变量

    dim3是NVIDIA的CUDA编程中一种自定义的整型向量类型,基于用于指定维度的uint3. 例如:dim3 grid(num1,num2,num3): dim3类型最终设置的是一个三维向量,三维参 ...

  8. 关于scanf()函数的一点理解

    习惯了c++的cin.cout之后,也不怎么关注空格,反正cin.cout会自动处理.有一次实验,创建Huffman树,要求输入空格字符,当时就懵逼了.cin咋输入空格呢? 没办法,只能重新用scan ...

  9. java变量和数据类型总结

  10. ueditor1.4.3 在IE8下的 BUG

    ueditor1.4.3  .net 版 在IE8 下,多图片上传完成后,点击确认时报错,无法插入图片到编辑器中 原因是 ueditor.all.js 中的 24835 行 if (whitList[ ...