0.9poj1129(dfs四色问题不错)

http://poj.org/problem?id=1129

Channel Allocation
Time Limit: 1000MSMemory Limit: 10000K
Total Submissions: 9779Accepted: 4983

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. 

Source

题意:给出一幅图,求最小染色数可以使得相邻两点不同色。即图的着色问题。

题目的描述部分翻译成中文大概就是:

当一个广播电台在一个非常大的地区,广播站会用中继器来转播信号以使得每一个接收器都能接收到一个强烈的信号。然而,每个中继器必须慎重选择使用,使相邻的中继器不互相干扰。如果相邻的中继器使用不同的频道,那么就不会相互干扰。

由于无线电频道是一有限的,一个给定的网络所需的中继频道数目应减至最低。编写一个程序,读取一个中继网络,然后求出需要的最低的不同频道数。

如果用图的术语来讲就是一个有N个节点的无向图,要求对每个节点进行染色,使得相邻两个节点颜色都不同,问最少需要多少种颜色?

那么题目就变成了一个经典的图的染色问题。

对于这题数据范围很小(节点最多26个),所以使用普通的暴力搜索法即可。

由四色定理可以得知最多需要4种颜色,所以可以枚举颜色的数目,然后判断是否可以染色。

解题思路:

1. 问题模型是着色问题,拿就枚举颜色的个数, 每次枚举看可以完成全部点的着色.

2. 采用深搜,每一次当前色的种数深搜完毕就加1种知道全部点都被着色完毕, 这样

从最少的开始深搜,结果出现肯定是最少的.

推荐博文:http://blog.csdn.net/lyy289065406/article/details/6647986

还有一种暴力解法http://blog.sina.com.cn/s/blog_626489680100kbon.html

这个也不错http://blog.163.com/boatswain@126/blog/static/1693964122010836141328/

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
bool map[30][30];
char s[30];
int color[30],color_count;
int n,mincolor=999999,flag;
bool isok(int dept,int  col)//判断某种颜色是否合适               
                           //判断在点dep着色col是否可以
 int  i;
 for(i=1;i<=n;i++)
 {
  if(map[dept][i]&&color[i]==col)//找与dep相邻的点,color[i]==col表示已经着过相同的颜色
   return false;                 如果该点与dept点相邻且,i点也染了该色,不可以.
 }
 return true;
}
void dfs(int depth)
{
 if(flag)
  return;
 if(depth==n+1)
 {
  mincolor=color_count;
  flag=1;
 }
 for(int k=1;k<=color_count;k++)//判断用过的颜色里是否有可用的
 {
  if(isok(depth,k))//返回则表示可用
  {
   color[depth]=k;//着色
   dfs(depth+1);
   color[depth]=0;//及时清除标记
  }
 }
 //上面不行的话在选用一种新的颜色
 color_count++;//颜色数+1
 color[depth]=color_count;
 dfs(depth+1);
 color[depth]=0;
 color_count--;
}
int main()
{
 while(scanf("%d",&n)!=EOF)
 {
  if(n==0)
   break;
  memset(map,0,sizeof(map));
  int  i,j;
  for(i=1;i<=n;i++)
  {
   getchar();   
   scanf("%s",s);
   int len=strlen(s);
   for(j=2;j<=len-1;j++)//把每组有邻接的点保存在邻接矩阵map[][]中;
    map[i][s[j]-'A'+1]=true;
  }
  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;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值