描述
 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.
 输入
 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.
 输出
 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.
 样例输入
 2
 A:
 B:
 4
 A:BC
 B:ACD
 C:ABD
 D:BC
 4
 A:BCD
 B:ACD
 C:ABD
 D:ABC
 0
 样例输出
 1 channel needed.
 3 channels needed.
 4 channels needed.
解题
题意大概是让相邻两个中继器,不能同波段,给你一个中继器和它相邻的中继器,求他们最少需要多少波段。
 可以想象成着色问题,相连的点不能用同一种颜色,问最少需要多少颜色。
 输入是中继器和和它相邻的中继器,将其转换成图的数组。我们只要让和他相邻的节点不同色即可
代码
#include <cstring>
#include <iostream>
using namespace std;
int map[30][30], col[30];
int n, min;
bool judge(int x, int color) {//判断此颜色能否填充
    for (int i = 0; i < n; i++) {
        if (map[x][i] && col[i] == color)
            return false;
    }
    return true;
}
void dfs(int x, int total) {
    if (x > n) {//如果超过
        if (total == 1)
            cout << "1 channel needed." << endl;
        else {
            cout << total << " channels needed." << endl;
        }
        return;
    }
    for (int i = 0; i <= total; i++) {
        if (i == total) {//如果i已经超过了颜色的数量
            col[x] = i;
            dfs(x + 1, total + 1);
            return;
        }
        if (judge(x, i)) {
            col[x] = i;
            dfs(x + 1, total);
            return;
        }
    }
}
int main() {
    char str[30];
    while (cin >> n && n) {
        memset(map, 0, sizeof(map));//初始化
        memset(col, -1, sizeof(col));
        for (int i = 0; i < n; i++) {
            cin >> str;
            int a = str[0] - 'A';
            int len = strlen(str);
            for (int j = 2; j < len; j++) {
                int b = str[j] - 'A';
                map[a][b] = 1;
                map[b][a] = 1;
            }
        }
        dfs(0, 1);
    }
}
 
                   
                   
                   
                   
                             该博客讨论了如何为广播覆盖广大区域的中继站网络有效地分配频道,以避免相邻中继站之间的干扰。通过将问题转化为图着色问题,作者提出了一种算法,该算法读取中继站及其相邻关系,并确定所需的最小频道数量。对于每个输入的中继站网络,程序会输出所需频道数,确保没有相邻频道相互干扰。示例输入和输出展示了算法的运行情况。
该博客讨论了如何为广播覆盖广大区域的中继站网络有效地分配频道,以避免相邻中继站之间的干扰。通过将问题转化为图着色问题,作者提出了一种算法,该算法读取中继站及其相邻关系,并确定所需的最小频道数量。对于每个输入的中继站网络,程序会输出所需频道数,确保没有相邻频道相互干扰。示例输入和输出展示了算法的运行情况。
           
       
           
                 
                 
                 
                 
                 
                
               
                 
                 
                 
                 
                
               
                 
                 扫一扫
扫一扫
                     
              
             
                   259
					259
					
 被折叠的  条评论
		 为什么被折叠?
被折叠的  条评论
		 为什么被折叠?
		 
		  到【灌水乐园】发言
到【灌水乐园】发言                                
		 
		 
    
   
    
   
             
            


 
            