Channel Allocation(POJ--1129

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.
题意:总共有n个字母,每一个字母表示一个接受信号的接收器,每个字母“:”后边可能有一系列字母,表示该字母与后边的字母相邻,对于接收器来说,如果相邻的接收器接受的是同一个信号则会有干扰,意味着相邻的接收器应该接受不同的信号,则该问题类似于给地图染色问题即给地图染色,而相邻的区域颜色不同,问最少用几种颜色。

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. 

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
#define MAX 0x3f3f3f3f
using namespace std;
int n;
bool flag;
char st[30];
bool  mmap[30][30];
int color[30],col;
bool dd(int i)
{
    for(int j=0; j<26; j++)
    {
    if(!mmap[i][j])
            continue;
   if(color[j]==color[i])   //如果i点与j点相邻且颜色相同则i点的着色不符合题意返回falsse
            return false;
    }
    return true;           //遍历所有的点之后都满足相邻两点间的颜色不同则返回true
}
void dfs(int sum)  
{
    if(sum==n)      //如果符合的点数已经达到要求的点数则返回
    {
        flag=true;
        return ;
    }
    for(int i=1; i<=col; i++)    //此时当前点只有col种颜色可选
    {
        color[sum]=i;            //让当前该点等于颜色i
        if(dd(sum))                //如果赋予当前点i颜色符合要求则接着枚举下一个点
            dfs(sum+1);
        color[sum]=0;          //恢复当前点的颜色即空白
    }
}
int main()
{
    //freopen("lalala.text","r",stdin);
    while(~scanf("%d",&n)&&n)
    {
        flag=false;
        memset(mmap,0,sizeof(mmap));
        for(int i=0; i<n; i++)           //开始构建地图
        {
            scanf("%s",st);
            int len=strlen(st);
            for(int j=2; j<len; j++)
                mmap[i][st[j]-'A']=mmap[st[j]-'A'][i]=1;    //这是一个无向边地图
        }
        for(col=1; col<=4; col++)   //对于给地图染色问题有一个四色定理,即给一个平面图染色最多用4种颜色即可使所有的相邻的区域不是同一个颜色。
        {
            dfs(0);        //从第一个点开始枚举
            if(flag)        //只要有符合的就跳出
                break;
        }
        if(col==1)
            printf("1 channel needed.\n");
        else
            printf("%d channels needed.\n",col);
    }
    return 0;
}<strong>
</strong>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值