码迷,mamicode.com
首页 > 其他好文 > 详细

poj 1129 搜索

时间:2014-08-18 12:27:24      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   java   os   io   

Channel Allocation

Time Limit: 1000 MS Memory Limit: 10000 KB

64-bit integer IO format: %I64d , %I64u Java class name: Main

[Submit] [Status] [Discuss]

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. 

#include <iostream>
#include <string.h>
#include <stdio.h>

using namespace std;
 /*int n;  ///n个广播站
 bool map[35][35];  ///个广播站之间的联系关系
 int ans;  ///需要多少广播站
 int color[35]; ///染色
 bool IsFind;*/

int n;
bool IsFind;
int ans;
int color[35];
bool map[35][35];
///两个版本定义有什么区别呢??????????????????


bool OK(int x,int c) ///判断相邻节点颜色是否相同
{
    for(int i=0;i<n;i++)
    {
        if(map[x][i]&&c==color[i])   ///id节点与各个节点比较  x与id节点有连边&&颜色重复了
        {
            return false;
        }
    }
    return true;
}

void DFS(int id,int total)  ///当前染色节点编号  总共用的颜色数量
{
    if(IsFind)
    return ;
    if(id>=n)
    {
        IsFind=true;
        return ;
    }

    for(int i=1;i<=total;i++)
    {
        if(OK(id,i))
        {
            color[id]=i;   ///符合条件就上色  之前的颜色
            DFS(id+1,total); ///继续加点
            color[id]=0; ///回溯
        }
    }
    if(!IsFind)   ///之前的颜色都不符合要求
    {
        ans++;  ///加点
        DFS(id,total+1);  ///加颜色
    }
}


int main()
{
    char str[35];
    while(scanf("%d",&n)!=EOF)
    {

        if(n==0)
        break;
        memset(map,false,sizeof(map));
        memset(color,0,sizeof(color));
        for(int i=1;i<=n;i++)
        {
            cin>>str;
            int len=strlen(str);
            for(int j=2;j<=len;j++)
            {
                map[str[0]-A][str[j]-A]=true;;
            }
        }
        IsFind=false;
        ans=1;
        DFS(0,1);
        if(ans==1)
        printf("1 channel needed.\n");
        else
        printf("%d channels needed.\n",ans);
    }
    return 0;
}

 

poj 1129 搜索,布布扣,bubuko.com

poj 1129 搜索

标签:des   style   blog   http   color   java   os   io   

原文地址:http://www.cnblogs.com/zhangying/p/3919203.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!