POJ 1129 四色原理+搜索

原创 2015年11月17日 16:50:56

Channel Allocation
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 13432 Accepted: 6874
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.

题意:
有几个点,这几个点每个要用一个通信频道,要求相邻的点之间频道不能相同。问最多有几个频道,注意单数输出和复出输出格式不一样。
解法:
最直观的想法就是对一个新点先判断和它相邻并且染过色的有那几种颜色(颜色从1开始编号)然后去除这几种颜色后,选一个能够染的颜色然后开始递归dfs,递归后把染的颜色取消掉(回溯)
这是最常规的解法,但是我们知道有一个四色定理:在一张地图上,最多只用四种颜色就可以对整个地图染色且相邻的区域颜色不同(虽然没有数学上严格的证明,但是通过现代计算机对世界地图的模拟,确实是对的。)那么我们的解法就可以变得更加直观:从第一点开始到第N个点,对每个点染色,只有1,2,3,4四种颜色,当对第i个点染色时,将冲突的颜色去掉,选择编号最小的染色。
dfs回溯+找到可行解后剪去其他枝也能达到和直接染色一样的复杂度。反过来说直接染色只是一种比较聪明的搜索而已:它避开了所有不可行的路径。这里贴上直接染色的代码:

#include <cstdio>
#include <algorithm>
#include <cstring>

using namespace std;

int G[30][30];
int P[30];

int main(){
//    freopen("data.in","r",stdin);
    int N;
    while(scanf("%d",&N)&&N){
        getchar();
        int i,j;
        char c,cs;
        memset(G,0,sizeof(G));
        for(i = 0;i<N;i++){
            scanf("%c:",&c);
            cs = getchar();
            while(cs!='\n'){
                G[c-'A'][cs-'A'] = G[cs-'A'][c-'A'] = 1;
                cs = getchar();
            }
        }

        memset(P,0,sizeof(P));
        for(i = 1;i<=N;i++){
            int a[10] = {0};
            for(j = 0;j<N;j++)
                if(G[i-1][j])
                    a[P[j+1]] = 1;
            for(j = 1;j<=4;j++)
                if(a[j] == 0){
                    P[i] = j;
                    break;
                }
        }
        int Max = -0x3f3f3f3f;
        for(i = 1;i<=N;i++)
            if(Max<P[i]) Max = P[i];

        if(1 == Max)
            printf("1 channel needed.\n");
        else
            printf("%d channels needed.\n",Max);
    }
    return 0;
}
版权声明:博客迁移到 yaokun.wiki

相关文章推荐

POJ Channel Allocation 1129(dfs+四色定理)

Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 1451...

POJ 1129 Channel Allocation (四色定理+DFS)

Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12678   Accep...

POJ 1129 Channel Allocation dfs(四色定理)

题意:在广播时,相邻的中继器会互相干扰,为了避免干扰,可以让中继器使用不同的通道。现在给出n个中继器,以及分别与它们相邻的中继器。求最少需要的通道数。 题解:给出两组代码,第二组用到了四色定理。 ...
  • Tsaid
  • Tsaid
  • 2011年09月07日 01:37
  • 957

poj 1129 四色定理

#include using namespace std; int map[27][27]; int n; int ans[27];//记录每个点的着色的情况 int ok(in...

poj1129Channel Allocation 乱做,dfs或+四色定理

Description When a radio station is broadcasting over a very large area, repeaters are used to retra...

Channel Allocation poj1129 (深搜/四色/枚举)

Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 1506...

POJ - 1129 Channel Allocation解题报告(涂色问题+四色定理)

题目大意: 模型化好像就是涂颜色,相连的点不能涂一个颜色。告诉你了哪些点相连。问你至少需要多少种颜色...

POJ 1129 Channel Allocation 涂色问题 DFS 回溯 搜索

题意:构图,如果两点互相影响,则连接一条边。 剩下的就是图着色问题,相连的点不能用同一种颜色,问最少需要多少颜色。 写法一:0ms #include using namespace std...

POJ 1129 Channel Allocation(搜索)

题目大意 给定一个图(V,E),V≤26(V,E),V\le 26,节点表示广播站,现在让你给每个广播站分配一个频道(用一个数字表示),使得相邻的广播站的频道数不同。问最少用多少种频道能满足要求。...
  • mmy1996
  • mmy1996
  • 2016年12月02日 22:47
  • 141
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:POJ 1129 四色原理+搜索
举报原因:
原因补充:

(最多只允许输入30个字)