Channel Allocation
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 13357   Accepted: 6836

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.

题意是给了一堆的信号辐射塔,然后给了各个塔之间的关系,A:BCD代表A与B塔,A与C塔,A与D塔相邻,这样的话A就不能与B、C、D塔中的任何一个使用相同的辐射频率。问在给定关系的情况下,最少使用多少种辐射频率。

和小时候接触的世界地图的染色方案一样,相邻国家不能使用同一种颜色。这样最多就4种颜色。

这道题折磨了我颇久,用dfs一直不对,结果看了discuss发现顺序染色是不对的。

看给定数据:

6
A:BEF
B:AC
C:BD
D:CEF
E:ADF
F:ADE
3 channels needed.
即:A(1)B(2)C(3)D(1)E(2)F(3)

贪心做法是不对的,还是暴力搜吧,反正一共就26个点,四种颜色换着法地涂,看最后最少能用多少种颜色。

代码:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std; int n, maxn;
string test;
int grid[30][30]; void dfs(int step,int color[], int cap)
{
if (step == n )
{
maxn = min(maxn, cap);
return;
}
int i, j;
for (i = 1; i <= 4; i++)//根据四色定理可知,一共四种颜色。所以暴力搜索,把所有符合条件的颜色都染着试一下
{
for (j = 0; j < 26; j++)
{
if (grid[step][j] == 1 && color[j] == i)
break;
}
if (j == 26)//如果这个颜色符合条件
{
color[step] = i;
dfs(step + 1, color, max(cap, i));
color[step] = 0;
}
}
} int main()
{
int i, j, k, len;
int color[30];
while (cin >> n)
{
if (n == 0)
break;
memset(grid, 0, sizeof(grid));
memset(color, 0, sizeof(color));
maxn = 10; for (i = 0; i < n; i++)
{
cin >> test;
len = test.length(); if (len == 2)
continue;
k = test[0] - 'A';
for (j = 2; j < len; j++)
{
grid[k][test[j] - 'A'] = 1;
grid[test[j] - 'A'][k] = 1;
}
}
dfs(0,color,0); if (maxn == 1)
{
cout <<"1 channel needed." << endl;
}
else
{
cout << maxn <<" channels needed. "<< endl;
}
}
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

POJ 1129:Channel Allocation 四色定理+暴力搜索的更多相关文章

  1. POJ 1129 Channel Allocation 四色定理dfs

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

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

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

  3. PKU 1129 Channel Allocation(染色问题||搜索+剪枝)

    题目大意建模: 一个有N个节点的无向图,要求对每个节点进行染色,使得相邻两个节点颜色都不同,问最少需要多少种颜色? 那么题目就变成了一个经典的图的染色问题 例如:N=7 A:BCDEFG B:ACDE ...

  4. POJ 1129 Channel Allocation(DFS)

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

  5. POJ 1129 Channel Allocation DFS 回溯

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

  6. poj 1129 Channel Allocation ( dfs )

    题目:http://poj.org/problem?id=1129 题意:求最小m,使平面图能染成m色,相邻两块不同色由四色定理可知顶点最多需要4种颜色即可.我们于是从1开始试到3即可. #inclu ...

  7. poj 1129 Channel Allocation

    http://poj.org/problem?id=1129 import java.util.*; import java.math.*; public class Main { public st ...

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

  9. poj 1054 The Troublesome Frog (暴力搜索 + 剪枝优化)

    题目链接 看到分类里是dp,结果想了半天,也没想出来,搜了一下题解,全是暴力! 不过剪枝很重要,下面我的代码 266ms. 题意: 在一个矩阵方格里面,青蛙在里面跳,但是青蛙每一步都是等长的跳, 从一 ...

随机推荐

  1. HiBench成长笔记——(10) 分析源码execute_with_log.py

    #!/usr/bin/env python2 # Licensed to the Apache Software Foundation (ASF) under one or more # contri ...

  2. springCloud 之 Eureka服务治理

    服务治理是微服务架构中最核心和基础的模块 首先我们创建一个springCloud eureka service的springboot 工程,该工程提供一个服务中心,用来注册服务,第二个工程是clien ...

  3. vue的自定义

    自定义组件 组件是可以被复用的页面的零件,其实就是一个插件,只是在vue里叫组件 先看看别人的组件 vant element Mint iView 去试试上面的组件,都是有脚手架版和直接引入使用的版本 ...

  4. 2020牛客寒假算法基础集训营4 I 匹配星星

    https://ac.nowcoder.com/acm/contest/3005/I 又做麻烦了,悲催... 将所有星星按x坐标为第一关键字,z为第二关键字排好序 那么一个z=1的星星匹配的是x比它小 ...

  5. 史上最全的mysql聚合函数总结(与分组一起使用)

    1.首先我们需要了解下什么是聚合函数 聚合函数aggregation function又称为组函数. 认情况下 聚合函数会对当前所在表当做一个组进行统计. 2.聚合函数的特点 1.每个组函数接收一个参 ...

  6. int类型和char类型的区别

    下面三个定义式的区别: int i = 1; char i = 1; char i = '1'; int用来定义整型变量,char用来定义字符型变量,要清楚的知道三个定义式的区别,可以比较它们在内存中 ...

  7. pl/sql远程连接oracle数据库乱码

    1. --在PLSQL Developer中查询select userenv('language') from dual ; 我的查询结果为:AMERICAN_AMERICA.ZHS16GBK 2.新 ...

  8. Centos 时间与主机时间不匹配问题解决

    有时候新安装的虚拟机的系统时间会和主机的时间差8个小时,这是因为虚拟机和主机所在的时区不同. 这会导致当你往mysql数据库插入数据时如果设置时间为current_time结果会和虚拟机保持一致. 1 ...

  9. 五十七、SAP中关于信息框的总结

    一.事务代码SE91 二.里面有我们创建过的一个消息类Z_TIANPAN_201907_MSG 三.这个消息类中有2个内容 四.我们的代码如下 五.S001效果 六.E001和W001效果 七.I00 ...

  10. Python MySQL Order By

    章节 Python MySQL 入门 Python MySQL 创建数据库 Python MySQL 创建表 Python MySQL 插入表 Python MySQL Select Python M ...