zoukankan      html  css  js  c++  java
  • POJ 1129 深搜&四色染图&模拟

    Channel Allocation
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 15241   Accepted: 7731

    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.


    题意:一套网络里面有转发器,转发器之间会有干扰,给出的数据是会干扰的,要用不同的频率来消除干扰,问最少的频率数目。

    思路:相当于双向图一样吧,可以看成图的染色问题,相邻点不能染同样色,问至少需要多少种颜色,可以直接爆搜或者用图论四色定理解决。但这里我直接用模拟就解决了。

    根据输入的字符串慢慢将图扩大,每次读取字符串需要重置mark。染色第一个点后,(理论上第一个字符串没什么用,因为是双向的,所以没什么影响),后面判断如果有这个点相邻,则标记当前点不能用相邻点的颜色。赋值的颜色从小到大赋值,最后取最大的颜色即可。注意颜色不从0开始是便于第一个点的染色。这里用到的sting当做字符数组用,要G++才能过,C++会CE。

    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    #include <cstring>
    using namespace std;
    const int maxn = 30;
    int color[maxn];//用数字给颜色赋值代号,便于统计颜色数量
    bool mark[maxn];//标记颜色
    int main() {
       string str;
       int n,i,j;
    
    
       while(~scanf("%d",&n)&&n){
    
    
            memset(color,0,sizeof(color));
    
    
       for(i=0; i < n; i++) {
        cin>>str;
        memset(mark,false,sizeof(mark));//关键初始化!因为是模拟的一个一个点找的,慢慢将图扩大,所以每次扩大一次图,应该将标记初始化一次
        for(j=2; j < str.length(); j++) {
            if(color[str[j]-'A'])//如果起始点已经有了颜色
                mark[color[str[j]-'A']] = true; //标记当前终点,说明当前i点已经不能再用color[str[j]-'A']这个颜色了
        }
    
    
        for(j = 1; j <= n; j++) { //枚举颜色,最大为n
            if(!mark[j]){
                color[i] = j; //给顶点i染上j颜色
                break;
            }
        }
    
    
       }
       int Max = 1;
       int num = 0;
       while(num!=n){
                if(Max<color[num])
                    Max = color[num];
                    num++;
       }
    //   for(i=0;i<n;i++){      //去重发统计,vis,1,2,3,1,访问过1颜色,就会标记1颜色,不再访问1颜色
    //            if(!vis[color[i]]){
    //                sum++;
    //                vis[color[i]]=1;
    //            }
    //        }
        if(Max==1)
            printf("%d channel needed.
    ",Max);
        else
            printf("%d channels needed.
    ",Max);
       }
       return 0;
    }
    


  • 相关阅读:
    java读写文本文件
    django学习<二>:连接数据库
    【MongoDB】递归获取字段更新表达式,更新复杂数据类型对象
    【MongoDB】C#中的Mongo数据类型转换
    【MongoDB】 基于C#官方驱动2.2版的封装类
    【Python】 属性的 get 与 set 方法
    【基础知识】UML基础
    【C#】 知乎用户网络爬虫
    【C#】MVC项目中搭建WebSocket服务器
    【MongoDB】 Windows 安装
  • 原文地址:https://www.cnblogs.com/zhangmingzhao/p/7256667.html
Copyright © 2011-2022 走看看