(POJ1129)Channel Allocation <涂色问题问最少颜色数 剪枝搜索 > || <四色定律>

87 篇文章 0 订阅
6 篇文章 0 订阅

Channel Allocation
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

Southern African 2001

题意:
给你一个图的相邻关系(A:BC 表示A和B,C相邻),问相邻的不能图同一种颜色,最少需要多少颜色数?

分析:
由于只有26个节点,所有我们可以通过暴力搜索+剪枝来完成
每次找出最小的可以用来图的颜色来图即可,先从已经用的中找,否则用下一种颜色

AC代码:

#include <iostream>
#include <cstring>
#include <string>
using namespace std;

const int MAXN = 26 + 2;
int map[MAXN][MAXN];
int Color[MAXN];
int N;//number of repeaters
int MinColor;
string Str;//information about every repeater

void DFS(int Step, int CurColor)//Curlor record how many color exist when coming into step Step
{
    if(Step == N)
    {
        if(CurColor < MinColor)
            MinColor = CurColor;
        return;
    }
    if(CurColor >= MinColor)  // 剪枝
        return;
    int flag = 0;
    for(Color[Step] = 1; Color[Step] <= CurColor; ++Color[Step])
    {
        if(flag) break;
        int flag1 = 0;
        for(int i = 0; i < Step; ++i)
        {
            if(map[Step][i] && Color[Step] == Color[i])
            {
                flag1 = 1;
                break;
            }
        }
        if(!flag1)
        {
            flag = 1;
            DFS(Step + 1, CurColor);
        }

    }
    if(!flag) DFS(Step + 1, CurColor + 1);//前面用过的颜色都不可用,用下一个
}

int main()
{
    while(cin >> N)
    {
        if(N == 0)
            break;
        cin.ignore();
        memset(map, 0, sizeof(map));
        memset(Color, -1, sizeof(Color));
        MinColor = 1000;
        for(int j = 1; j <= N; ++j)
        {
            getline(cin, Str);
            int row;
            int col;
            if(Str.length() > 2)
            {
                row = Str[0] - 'A';
                for(int i = 2; i < Str.length(); ++i)
                {
                    col = Str[i] - 'A';
                    map[row][col] = map[col][row] = 1;
                }
            }
        }
        Color[0] = 1;
        DFS(1, 1);
        if(MinColor == 1)
            cout << "1 channel needed." << endl;
        else
            cout << MinColor << " channels needed." << endl;
    }
    return 0;
}

四色定律:四种颜色可以图所有的地图,是相邻的不同色
我们用int color[i]来表示第i各节点的颜色,其中int占4个字节,每个字节表示一种颜色。每次找出最小的可用的颜色来图:lowbit(x) 函数的作用是从右开始找出第一个为1的字节的位置,然后告诉相邻的所有节点该颜色不可用,即将相应为置0.

AC代码:

#include <stdio.h>
#include <string.h>
bool grid[26][26];
int color[26];
int lowbit(int x)
{
    return x&(-x);
}
int getColorNum(int x)
{
    switch(x)
    {
        case 0x1:return 1;
        case 0x2:return 2;
        case 0x4:return 3;
        case 0x8:return 4;
    }
}
int main()
{
    char c;
    int n,ans,tmp;
    while(scanf("%d\n",&n)&&n)
    {
        ans=0;
        memset(grid,false,sizeof(grid));
        for(int i=0;i<n;++i)
        {
            getchar();
            getchar();
            while((c=getchar())!='\n')
                grid[i][c-'A']=true;
            color[i]=0xf;
        }
        for(int i=0;i<n;++i)
        {
            color[i]=lowbit(color[i]);
            tmp=getColorNum(color[i]);
            if(tmp>ans)
                ans=tmp;
            if(ans==4)
                break;
            for(int j=i+1;j<n;++j)
                if(grid[i][j])
                    color[j]^=color[i];
        }
        ans==1?printf("%d channel needed.\n",ans):printf("%d channels needed.\n",ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值