关闭

POJ 1129 四色原理+搜索

标签: poj
118人阅读 评论(0) 收藏 举报
分类:

Channel Allocation
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 13432 Accepted: 6874
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.

题意:
有几个点,这几个点每个要用一个通信频道,要求相邻的点之间频道不能相同。问最多有几个频道,注意单数输出和复出输出格式不一样。
解法:
最直观的想法就是对一个新点先判断和它相邻并且染过色的有那几种颜色(颜色从1开始编号)然后去除这几种颜色后,选一个能够染的颜色然后开始递归dfs,递归后把染的颜色取消掉(回溯)
这是最常规的解法,但是我们知道有一个四色定理:在一张地图上,最多只用四种颜色就可以对整个地图染色且相邻的区域颜色不同(虽然没有数学上严格的证明,但是通过现代计算机对世界地图的模拟,确实是对的。)那么我们的解法就可以变得更加直观:从第一点开始到第N个点,对每个点染色,只有1,2,3,4四种颜色,当对第i个点染色时,将冲突的颜色去掉,选择编号最小的染色。
dfs回溯+找到可行解后剪去其他枝也能达到和直接染色一样的复杂度。反过来说直接染色只是一种比较聪明的搜索而已:它避开了所有不可行的路径。这里贴上直接染色的代码:

#include <cstdio>
#include <algorithm>
#include <cstring>

using namespace std;

int G[30][30];
int P[30];

int main(){
//    freopen("data.in","r",stdin);
    int N;
    while(scanf("%d",&N)&&N){
        getchar();
        int i,j;
        char c,cs;
        memset(G,0,sizeof(G));
        for(i = 0;i<N;i++){
            scanf("%c:",&c);
            cs = getchar();
            while(cs!='\n'){
                G[c-'A'][cs-'A'] = G[cs-'A'][c-'A'] = 1;
                cs = getchar();
            }
        }

        memset(P,0,sizeof(P));
        for(i = 1;i<=N;i++){
            int a[10] = {0};
            for(j = 0;j<N;j++)
                if(G[i-1][j])
                    a[P[j+1]] = 1;
            for(j = 1;j<=4;j++)
                if(a[j] == 0){
                    P[i] = j;
                    break;
                }
        }
        int Max = -0x3f3f3f3f;
        for(i = 1;i<=N;i++)
            if(Max<P[i]) Max = P[i];

        if(1 == Max)
            printf("1 channel needed.\n");
        else
            printf("%d channels needed.\n",Max);
    }
    return 0;
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:30652次
    • 积分:2024
    • 等级:
    • 排名:第19635名
    • 原创:179篇
    • 转载:1篇
    • 译文:0篇
    • 评论:2条
    文章分类
    最新评论