2019-02-24-Channel Allocation-POJ1129-图着色问题-四色问题-dfs

POJ-1129-Channel Allocation

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.

题目简述

对于给定的一组广播电台(A,...,),已知所有电台都在一个平面内,每个电台的辐射范围给出(能和哪些电台共享频道),求最少需要的频道数

分析:题意即在平面内有多块区域,不同区域之间有相邻部分,用不同颜色进行染色,使得用最少的颜色来覆盖所有区域,并且每个相邻区域的颜色不同

条目检索:四色定理

输入输出

输入:多组数据,字母转换数字,字符串格式读取一行

输出:结果+语句+标点,单复数

使用模板(无)

AC代码

//dfs+四色定理
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n;
int mp[27][27];
int v[27];          //元素i上所涂的颜色
bool fg;            //用于退出dfs迭代
void dfs(int x){    //当前进行深搜的元素为x(1,2,...,x,...,n),或者现在正在进行第x次dfs
	if(x==n+1){     //当前为第n+1次dfs,即到达终点
		fg=1;       //可以结束dfs
		int ans=0;
		for(int i=1;i<=n;i++){
			ans=max(ans,v[i]);    //把ans设置为最大的颜色序号,即最大颜色数
		}

		if(ans==1)printf("%d channel needed.\n",ans);
		else printf("%d channels needed.\n",ans);

		return;
	}
	for(int i=1;i<=4;i++){        //根据四色定理,只要搜索4种情况即可
		int j;
		for(j=1;j<=n;j++){
			if(mp[x][j]&&v[j]==i)break;    //寻找和x相邻,而且颜色相同
		}
		if(j==n+1){               //如果没有找到,那么就把x染成颜色i(1,2,3,4)
			v[x]=i;               //更改x的颜色
			dfs(x+1);             //进行下一元素的染色
			if(fg)return;         //如果全部涂完,那么快速退出dfs
		}
	}
}
int main(){
	while(scanf("%d",&n)&&n){
		fg=0;
		memset(mp,0,sizeof(mp));
		memset(v,0,sizeof(v));
		char s[30];

		for(int i=0;i<n;i++){
			scanf("%s",s);
			int x=s[0]-'A'+1;
			int len=strlen(s);
			for(int j=2;j<len;j++){
				int y=s[j]-'A'+1;
				mp[x][y]=mp[y][x]=1; 
			}
		}

		dfs(1);                  //进行深搜,并且输出
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值