POJ 1128 Channel Allocation

Channel Allocation
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 10047 Accepted: 5118

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. 

Source

解题思路:先转化为对于地图上的n个区域,在相邻区域不能用同一种颜色涂色的情况下,求最少要用多少种颜色的涂色问题,开始不知道用搜索,上网看了别人的解题报告,思路是这样子的,先初始化所有节点颜色为0,从最少用1种颜色依次向后搜索,搜索一个节点时,若此节点染色后与其相邻节点颜色均不同,就将其染成此颜色,否则换一种新颜色,当此次搜索的颜色用完而节点仍无法染色时,对上一个节点重新搜索,如果退到第一个节点都无法完成染色,就添加一种颜色,从头开始染色。
#include<iostream>
using namespace std;
bool link[30][30];
int color[30];
int n;
bool Pass(int col,int p)
	//遍历该节点的所有相邻节点,若其颜色与相邻节点均不同,返回true,否则返回false
{
	for(int i=0;i<n;i++)
		if(link[i][p]&&col==color[i])
			return false;
	return true;
}
bool Dfs(int c,int pos)
{
	if(pos==n) return true;//搜索到最后一个节点,搜索完成
	for(int i=1;i<=c;i++)
		if(Pass(i,pos))
		{
			color[pos]=i;//如果该节点满足条件,将该节点设为当前颜色
			if(Dfs(c,pos+1)) return true;//看下一个节点能否仍用该颜色,不行的话就设为一种新颜色,直到本次搜索的颜色用完
			color[pos]=0;//下一节点无法满足要求时,对本节点重新进行搜索,直到本次搜索颜色用完
		}
	return false;
}
int main()
{
	char net[30];
	int ans;
	while(cin>>n&&n)
	{
		memset(color,0,sizeof(color));//初始化所有节点颜色为0
		memset(link,false,sizeof(link));
		for(int i=0;i<n;i++)
		{
			cin>>net;
			for(int j=2;net[j]!='\0';j++)
				link[i][net[j]-'A']=true;
		}
		for(ans=1;ans<=n;ans++)//从至少1种颜色向后依次搜索
			if(Dfs(ans,0)) break;
		if(ans==1)
		    cout<<ans<<" channel needed.\n";
		else
			cout<<ans<<" channels needed.\n";
	}
	return 0;
}
 

然后还有一种思路是考虑到最多只有四个区域可以全部相邻,就是说最多只要四种颜色就可以涂满地图,所以暴力枚举就可以了
                                      
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
const int N=30;

bool maps[N][N];
int n;
void init()//读入数据并及初始化工作
{
 memset(maps,false,sizeof(maps));
 char ch[N];
 for(int i=0;i<n;i++)
 {
  cin>>ch;
  for(int j=2;j<strlen(ch);j++)
  {
   maps[ch[0]-'A'][ch[j]-'A']=1;
   maps[ch[j]-'A'][ch[0]-'A']=1;
  }
 }
}

void solve()
{
 int i,j,k,q;
 //如果出现四个区域,每个区域都与其他三个邻接  
    for(i=0;i<n;i++)  
        for(j=0;j<n;j++)  
            for(k=0;k<n;k++)  
                for(q=0;q<n;q++)  
                    if(maps[i][j]&&maps[i][k]  
                        &&maps[i][q]&&maps[j][k]  
                        &&maps[j][q]&&maps[k][q])  
                    {  
                        cout<<"4 channels needed."<<endl;  
                        return;  
                    }
 //如果有三个区域两两邻接,要用三种颜色  
    for(i=0;i<n;i++)  
        for(j=0;j<n;j++)  
            for(k=0;k<n;k++)  
                if(maps[i][j]&& maps[i][k] && maps[j][k])  
                {  
                    cout<<"3 channels needed."<<endl;  
                    return;  
                }
 //如果存在两个区域邻接的情况,要用2种颜色  
    for(i=0;i<n;i++)  
        for(j=0;j<n;j++)  
            if(maps[i][j])  
            {  
                cout << "2 channels needed." << endl;  
                return;  
            }
 //没有区域邻接  
    cout << "1 channel needed." << endl;  

}

int main()
{
 while(cin>>n && n)
 {
  init();
  solve();
 }
 return 0;
} 


 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值