正文

Channel Allocation 2007-08-13 17:31:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/lingdlz/28467.html

分享到:

Channel Allocation
Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:32768KB
Total submit users: 6, Accepted users: 6
Problem 10821 : No special judgement
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 10, 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.

/// 题目意思: 大写字母代表中继站,相邻的中继站不能使用同一频道

#include <stdio.h>
#include <string.h>
#define MAX 20
char str[MAX][MAX],tmp[MAX+4],tag;
int test(int i, int j){
    int k;
    for(k=1; k<=str[i][0]; k++)
        if(str[i][k]==j)
            return 0;
    return 1;
}
int testTwo(int k,int j){
    int i;
    for(i=1; i<=str[k][0]; i++)
        if(tmp[str[k][0]]==tmp[j])
            return 0;
    return 1;
}
void myFill(int k){
    int j;
    for(j=0; j<MAX; j++){
        if(tmp[j] && test(k,j) && testTwo(k,j)){
            tmp[k]=tmp[j];
            break;
        }
    }
    if(!tmp[k])
        tmp[k]=tag++;
}
void get(int k){
    int i;
    if(!tmp[k])
        myFill(k);
    for(i=1; i<=str[k][0]; i++){
        if(!tmp[str[k][i]])
            myFill(str[k][i]);
    }
}
int main(){
    int  n,i,j;
    while(1){
        scanf("%d",&n);
        if(!n)
            break;
        for(i=0; i<n; i++){
            scanf("%s",tmp);
            for(j=2; tmp[j]; j++)
                str[i][j-1]=tmp[j]-'A';
            str[i][0]=j-2;
        }
        memset(tmp,0,sizeof(char)*(MAX+2));
        for(tag=1,i=0; i<n; i++)
            get(i);
        if(tag==2)
            printf("1 channel needed.\n");
        else
            printf("%d channels needed.\n",tag-1);
    }
    return 0;
}

阅读(5096) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册