POJ1129-Channel Allocation(DFS图的着色)

23 篇文章 0 订阅

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. 

题意:

一个广播电台在一个非常大的地区,广播站会用中继器来转播信号以使得每一个接收器都能接收到一个强烈的信号。然而,每个中继器必须慎重选择使用,使相邻的中继器不互相干扰。如果相邻的中继器使用不同的频道,那么就不会相互干扰。由于无线电频道是有限的,一个给定的网络所需的中继频道数目应减至最低。 编写一个程序,读取一个中继网络,然后求出需要的最低的不同频道数。

样例输入的意思是,A:BC,A与BC相邻,使相邻结点频率不会受到干扰。

这道题不用四色原理也可以过,因为数据少。

四色定理的“相邻”是指两块多边形地区“至少一条边重合”才为之相邻

“至少一条边重合”同时也隐含了“任意边(线段)不正规相交

举个栗子:

N=7

A:BCDEFG

B:ACDEFG

C:ABD

D:ABCE

E:ABDF

F:ABEG

G:ABF

画图

四色原理参考下方

https://baike.baidu.com/item/%E5%9B%BE%E7%9D%80%E8%89%B2%E9%97%AE%E9%A2%98?fr=aladdin

暴力搜索代码:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define M 26
int n,ans,color[M];
bool map[M][M],find;
bool ok(int x, int c)
{   //判断是否存在相邻节点颜色相同
     for(int i=0;i<n;i++)
        if(map[x][i]&&c==color[i])
         return false;
        return true;
}

void DFS(int id,int total)
{   //当前搜索到下标为id的节点,此时总共用的色彩数为total
     if(find)
        return;
     if(id >= n)
        {
            find=true;
             return;
             }  //当所有节点都被着色后,返回

     for(int i=1;i<=total;i++)
     {
         if(ok(id,i))
            {
            color[id]=i;
            DFS(id+1,total);
            color[id]=0;
         }
     }
     if(!find)
        {    //当用total种颜色无法完成时,则增加一种颜色进行着色
        ans++;
        DFS(id,total+1);
     }
}

int main()
{
    int i,j;
    char s[M];
    while(~scanf("%d", &n)&&n)
        {
          getchar();
          memset(map,false,sizeof(map));
          memset(color,0,sizeof(color));
          for(i=0;i<n;i++)
            {
               gets(s);
               for(j=2;s[j]!='\0';j++)
                   map[s[0]-'A'][s[j]-'A']=true;
          }
          find=false;
          ans=1;
          DFS(0,1);
          if (ans==1)
              printf("1 channel needed.\n");
          else
            printf("%d channels needed.\n",ans);
    }
    return 0;
}

四色原理代码:

#include<iostream>
using namespace std;
 
typedef class
{
	public:
		int next[27];  //直接后继
		int pn;   //next[]指针(后继个数)
}point;
 
int main(int i,int j,int k)
{
	int n;
	while(cin>>n)
	{
		if(!n)
			break;
 
		getchar();  //n的换行符
 
		point* node=new point[n+1];  //结点
 
		/*Structure the Map*/
 
		for(i=1;i<=n;i++)
		{
			getchar();  //结点序号
			getchar();  //冒号
 
			if(node[i].pn<0)   //初始化指针
				node[i].pn=0;
 
			char ch;
			while((ch=getchar())!='\n')
			{
				j=ch%('A'-1);   //把结点字母转换为相应的数字,如A->1  C->3
				node[i].next[ ++node[i].pn ]=j;
			}
		}
 
		int color[27]={0};  //color[i]为第i个结点当前染的颜色,0为无色(无染色)
		color[1]=1;  //结点A初始化染第1种色
		int maxcolor=1;  //当前已使用不同颜色的种数
 
		for(i=1;i<=n;i++)  //枚举每个顶点
		{
			color[i]=n+1;  //先假设结点i染最大的颜色
			bool vist[27]={false};  //标记第i种颜色是否在当前结点的相邻结点染过
			for(j=1;j<=node[i].pn;j++) //枚举顶点i的所有后继
			{
				int k=node[i].next[j];
				if(color[k])  //顶点i的第j个直接后继已染色
					vist[ color[k] ]=true;  //标记该种颜色
			}
			for(j=1;i<=n;j++)  //从最小的颜色开始,枚举每种颜色
				if(!vist[j] && color[i]>j) //注意染色的过程是一个不断调整的过程,可能会暂时出现大于4的颜色
				{                          //因此不能单纯枚举4种色,不然会WA
					color[i]=j;
					break;
				}
 
			if(maxcolor<color[i])
			{
				maxcolor=color[i];
				if(maxcolor==4)   //由四色定理知,最终完成染色后,图上最多只有四种颜色
					break;        //因此当染色过程出现结点的颜色为4时,就可以断定最少要用4种颜色染色
			}
		}
 
		if(maxcolor==1)
			cout<<1<<" channel needed."<<endl;
		else
			cout<<maxcolor<<" channels needed."<<endl;
 
		delete node;
	}
	return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值