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;
}

poj 1129 四色原理DFS

如题:     用中继器(repeater)给每个接受者(receiver)发送信号,为了防止信号干扰,两个相邻的广播站之间的中继器要不相同。问至少需要多少个中继器。...
  • Twenty_seven
  • Twenty_seven
  • 2014年11月02日 10:41
  • 712

poj 1129 四色定理

#include using namespace std; int map[27][27]; int n; int ans[27];//记录每个点的着色的情况 int ok(in...
  • ChallenChenZhiPeng
  • ChallenChenZhiPeng
  • 2012年07月22日 21:17
  • 583

POJ 1129 四色定理+spfa

题意: 最多有26个字母 n行表示有n个字母 下面n行给出 该字母和哪些字母相邻 问:至少要几种颜色染色后相邻字母不同色   四色定理至多4种 暴力枚举 #include #inc...
  • qq574857122
  • qq574857122
  • 2013年11月24日 18:43
  • 1076

poj 1129(DFS+四色定理)

之前练习二分图的时候有接触过染色法,用01染色法判断一个图是否是二分图,而这道题是进行图的染色,求最少最需要的着色数。 第一次接触到四色定理,对于任何一个无向图着色,最多只需要4种颜色即可,具体证明...
  • rachelsg
  • rachelsg
  • 2016年02月05日 00:16
  • 264

poj 1129(ID-DFS四色定理)

Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11099   Accept...
  • A775700879
  • A775700879
  • 2013年11月01日 22:01
  • 708

poj1129 dfs

题意:给定无向图,对点着色,使得相邻的结点颜色不同。 直接搜索,没有用四色原理,用四色原理也是暴力,没有什么影响。 #include #include #include using na...
  • Non_Cease
  • Non_Cease
  • 2012年03月02日 20:43
  • 2004

地图四色原理

http://www.360doc.com/content/13/0220/14/3046928_266739406.shtml http://blog.csdn.net/very_caiing/a...
  • dreamgis
  • dreamgis
  • 2017年07月12日 10:18
  • 661

poj 1129 搜索

//问题描述,这是一道典型的涂色问题,根据四色定理,最多不会超过4中颜色,所以只需要 //枚举1-3种颜色就行了 //对每一个节点来说,用k=1-cor颜色开始涂色,判断每种颜色是否可以使用,如果...
  • chen895281773
  • chen895281773
  • 2012年11月26日 15:17
  • 228

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

Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 1451...
  • ky961221
  • ky961221
  • 2016年08月18日 15:32
  • 214

POJ 1129:Channel Allocation 四色定理+暴力搜索

Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13357   Accept...
  • u010885899
  • u010885899
  • 2015年09月08日 10:16
  • 311
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:POJ 1129 四色原理+搜索
举报原因:
原因补充:

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