当前位置: 代码迷 >> 综合 >> poj 1129 Channel Allocation(dfs)
  详细解决方案

poj 1129 Channel Allocation(dfs)

热度:7   发布时间:2024-01-14 21:50:49.0
Channel Allocation
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 14326   Accepted: 7302

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<stdio.h>
#include<string.h>
char Map[27][27];//
int map[27][27];//转化为无向图
int c[27];//记录颜色
int m,n;
int ans;//记录步数
//int tol;
int flag;
int judge(int x,int color)//判断相邻是不是同色
{for(int i=0;i<n;i++){if(map[x][i]&&color==c[i])return 0;}return 1;
}
void dfs(int id,int tol)//id表示着色的的编号,tol表示染色的数目
{int i;if(flag) return;if(id>=n){ans=tol;flag=1;return;}for(i=1;i<=tol;i++){if(judge(id,i))//如果染色成功{c[id]=i;dfs(id+1,tol);}}if(i==tol+1)//若果染色不成功{dfs(id,tol+1);}
}
int main()
{int t;int i,j,k;while(~scanf("%d",&n),n){memset(map,0,sizeof(map));memset(c,0,sizeof(c));for(i=0;i<n;i++){scanf("%s",Map[i]);int len = strlen(Map[i]);int x=Map[i][0]-'A';for(j=2;j<len;j++){int y=Map[i][j]-'A';map[x][y]=1;}}flag=0;dfs(0,1);if(ans==1)printf("1 channel needed.\n");elseprintf("%d channels needed.\n",ans);}return 0;
}


  相关解决方案