首页 > 代码库 > poj 1129 搜索
poj 1129 搜索
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.
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.
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
2A:B:4A:BCB:ACDC:ABDD:BC4A:BCDB:ACDC:ABDD:ABC0
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;}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。