博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj 1129 也算是遍历的吧 两种方法
阅读量:7117 次
发布时间:2019-06-28

本文共 4946 字,大约阅读时间需要 16 分钟。

     我当时是不懂题目是什么意思的,看了别人的才知道原来是所谓的四色问题。我也不知道是什么东东,查了才懂。要学的还有很多呢!太弱了。关键把结构存储进去后,每次遍历它的邻接节点。并把用的颜色标记下来。然后再每次都要遍历每种颜色,如果这种颜色不是邻接的,那么久可以把它给用上。用col[]数组来表示总共用了多少种颜色。好了,代码里也标记得很清楚了。这题还有其它方法,要好好研究研究。  感谢。推荐大家看他的博客。

                                                                                   Channel Allocation
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 9268   Accepted: 4711

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

2A:B:4A:BCB:ACDC:ABDD:BC4A:BCDB:ACDC:ABDD:ABC0

Sample Output

1 channel needed.3 channels needed.4 channels needed.

Source

#include 
#include
using namespace std; bool vis[27]; int col[27]; //节点对应使用的颜色 col[i]=j.表示节点i使用了颜色j。 typedef class{
public: int next[27]; int pn; }point; int n; void getmap() {
} int main() {
freopen("acm.txt","r",stdin); while(scanf("%d",&n) && n) {
int i,j,maxcolor=1; memset(col,0,sizeof(col)); point *node=new point[n+1]; 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; } } for(i=1; i<=n; i++) {
col[i]=n+1; memset(vis,false,sizeof(vis)); for(j=1; j<=node[i].pn; j++) //判断某种颜色是否被邻接节点使用 {
if(col[node[i].next[j]]) vis[ col[node[i].next[j]] ]=true; //把这种颜色标记一下 } for(j=1; j<=n; j++) //遍历每种颜色 {
if(!vis[j] && col[i]>j) //某种颜色没有被邻接节点使用 {
col[i]=j; break; } } if(maxcolor
 
DFS:
#include 
#include
#include
using namespace std; bool map[27][27]; int color[27],color_count; int n,mincolor,flag; char s[27]; bool isOK(int dept,int col) { //判断某种颜色是否合适 for(int i=1; i<=n ;i++) { if(map[dept][i] && color[i]==col) {
return false; } } return true; } void dfs(int depth) {
if(flag) return; if(depth==n+1) { mincolor=color_count; flag=1; } for(int j=1; j<=color_count; j++) //判断用过的颜色里是否有可用的 {
if(isOK(depth,j)) {
color[depth]=j; dfs(depth+1); color[depth]=0; } } //选用一种新的颜色 color_count++; color[depth]=color_count; dfs(depth+1); color[depth]=0; color_count--; } int main(){
freopen("acm.txt","r",stdin); while(scanf("%d",&n)!=EOF && n) {
memset(map,0,sizeof(map)); //get the map for(int i=1; i<=n; i++) {
getchar(); scanf("%s",s); for(int j=2; s[j]!='\0'; j++) map[i][s[j]-'A'+1]=true; } mincolor=999; flag=0; color_count=1; memset(color,0,sizeof(color)); dfs(1); if(mincolor==1) printf("%d channel needed.\n",mincolor); else printf("%d channels needed.\n",mincolor); } return 0; }

转载地址:http://nqfel.baihongyu.com/

你可能感兴趣的文章
ElasticSearch Tune for disk usage Translation
查看>>
Redisson 成为 GitHub 里星星最多的 Redis Java 客户端
查看>>
汉得宣布开源:基于容器的企业级应用 PaaS 平台
查看>>
Python爬虫之微打赏爬虫
查看>>
轻松地在 Confluence 4.2 规划文档结构
查看>>
边缘计算的“完美风暴”
查看>>
贵州设立工业及省属国有企业绿色发展基金 总规模300亿
查看>>
springboot 2.X jdbc 实现session共享mysql
查看>>
第十四章:SpringCloud 依赖Git仓库实现配置对称加密
查看>>
1024 程序员节:给 DBA 们的福音
查看>>
寻找复杂背景下物体的轮廓(OpenCV / C++ - Filling holes)
查看>>
52ABP模板 ASP.Net Core 与 Angular的开源实例项目
查看>>
VC 6.0下载 VC 6.0英文版下载 Visual C++ 6.0 英文企业版 集成SP6完美版(最新更新地址,百度网盘)...
查看>>
递归-汉诺塔
查看>>
SAMtools: SAM格式的处理利器
查看>>
AI工程师成长之路--机器学习之模型评估与选择
查看>>
菜鸟排查数据库异常的事
查看>>
CSS:关于元素宽度与高度的讨论 系列文章(一)
查看>>
webstorm、phpstorm、idea等使用技巧记录
查看>>
腾讯内核团队发布 TCPA,为何是 OPEN 而非开源?
查看>>