POJ 1129:Channel Allocation 四色定理+暴力搜索

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;
}


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

转载于:https://www.cnblogs.com/lightspeedsmallson/p/4899596.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值