poj1129 Channel Allocation DFS

题目

题意:给定N个节点,让你对其涂色,使其任何相邻的两个节点颜色不同。

思路:

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

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

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

该题N<26,可以不用四色原理剪枝。

附上一发代码:

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cstring>
 4 #include<algorithm>
 5 using namespace std;
 6 int flag,ans,map[30][30],color[30],n;
 7 char s[40];
 8 int check(int x,int y)         //判断是否存在相邻节点颜色相同
 9 {
10     for(int i=0;i<n;i++)
11     {
12         if(map[x][i]&&color[i]==y)
13         return 0;
14     }
15     return 1;
16 }
17 void dfs(int a,int b)       //当前搜索到下标为a的节点,此时总共用的色彩数为b
18 {
19     if(flag)
20     return;
21     if(a>=n)                //当所有节点都被着色后,返回
22     {
23     flag=1;
24     return;
25     }
26     for(int i=1;i<=b;i++)        //搜索一种颜色能涂的所有情况
27     {
28         if(check(a,i))
29         {
30             color[a]=i;
31             dfs(a+1,b);
32             color[a]=0;     //回溯
33         }
34     }
35     if(!flag)             //当用b种颜色无法完成时,则增加一种颜色进行着色
36     {
37         ans++;
38         dfs(a,b+1);
39     }
40 }
41 main()
42 {
43     int i,j;
44     while(~scanf("%d",&n)&n)
45     {
46         memset(map,0,sizeof(map));
47         memset(color,0,sizeof(color));
48         for(i=0;i<n;i++)
49         {
50             scanf("%s",s);
51             for(j=2;s[j]!=‘\0‘;j++)      //for(j=2;j<strlen(s);j++)
52             map[s[0]-‘A‘][s[j]-‘A‘]=1;
53         }
54         flag=0;ans=1;
55         dfs(0,1);
56         if(ans==1)
57         printf("1 channel needed.\n");
58         else
59         printf("%d channels needed.\n",ans);
60     }
61 }
时间: 2024-08-26 18:01:06

poj1129 Channel Allocation DFS的相关文章

poj1129 Channel Allocation(染色问题)

题目链接:poj1129 Channel Allocation 题意:要求相邻中继器必须使用不同的频道,求需要使用的频道的最少数目. 题解:就是求图的色数,这里采用求图的色数的近似有效算法——顺序着色算法(实质是一种贪心策略:在给任何一个顶点着色时,采用其邻接顶点中没有使用的,编号最小的颜色). 注:中继器网络是一个平面图,即图中不存在相交的边. 看讨论后发现这组数据,AC代码没过orz: 6 A:BEF B:AC C:BD D:CEF E:ADF F:ADE 正确答案应该是3 : A(1)B(

快速切题 poj1129 Channel Allocation

Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12334   Accepted: 6307 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 s

POJ 1129 Channel Allocation DFS 回溯

Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 15546   Accepted: 7871 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 s

POJ-1129 Channel Allocation (DFS)

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 repea

poj1129 Channel Allocation 染色问题

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 repea

poj 1129 Channel Allocation (dfs)

链接:poj 1129 题意:如果相邻的中继器使用不同的频道,就不会相互干扰. 给定一些中继器的相邻关系,问至少要选几个不同的频道,使得中继器都不互相干扰. 分析:这题可以转化为无向图的染色问题, 即相邻的点不能染同一种颜色,求至少需要的几种颜色? 本题顶点数最多为26,可以直接用暴力搜索即可 思路:若顶点总数为n,则最多需要n种颜色(编号为1,2...n), 从最小的顶点开始染色,每次将与该顶点相邻的已染色的颜色标记, 再从未标记(未用)的颜色中,选出一个最小的颜色,给该点染色, 所有点染色完

POJ1129 Channel Allocation

题意 给定一个无向图,用最少的颜色来涂色,使所有相邻的点颜色都不重复. 思路 数据很小,暴力即可.或者直接贪心(总是选择能选择的id最小的颜色),下面的代码就是贪心做的. 代码 #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 30; const int INF = 1000000000; int cl[maxn]; int n;

Channel Allocation (poj 1129 dfs)

Language: Default Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12367   Accepted: 6325 Description When a radio station is broadcasting over a very large area, repeaters are used to retransmit the signal so that ever

POJ 1129 Channel Allocation(DFS)

Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13173   Accepted: 6737 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 s