poj 1129 Channel Allocation

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. 


这道题比赛的时候没看懂,后来问了学长,知道是4色问题,就是一张地图,分为很多块,去不同颜色涂,使得相邻区域的颜色不相同,最多只需要4种不同的颜色

那么这题就是给一张无向图,给点涂色,相邻点不能同颜色,问要多少种,如果图没有边,那就只需要1种,有边的话,就从2种颜色到4种颜色去涂点,这个操作就需要

用dfs去实现,就是给每个点从1到n标记,n是从2到4的,代表不同颜色,若不能符合情况,就中断,令需要的颜色种类加一,继续跑dfs,一直跑,若能够跑完全图,就是

涂完每个点,就是代表只需要这些数量的颜色就可以了,这里有个小技巧,就是如果3种颜色都不够,那就肯定是4种颜色了,就不用跑4种颜色的情况了。


#include<cstdio>  
#include<cstring> 
#include<iostream>
using namespace std;
int n,ma[27][27],color[27];  
int dfs(int id,int color1)  
{  
    int i,j,flag;  
    for(i=1;i<=color1;i++)  
    {  
        color[id]=i;     
        flag = 1;
        for(j=1;j<id;j++)  
        {  
            if(ma[j][id]&&color[j]==color[id])
            {  
                flag = 0;      
                break;  
            }  
        }  
        
        if(flag && (id == n || dfs(id+1,color1)))  
            return 1;  
    }  
    return 0;  
}  
  
int main()  
{  
    char s[30];  
    int i,j,ans;  
    while(scanf("%d",&n)!=EOF&&n)  
    {  
        ans=1; 
        memset(ma,0,sizeof(ma));  
        
        for(i=1;i<=n;i++)  
        {  
            cin>>s;  
            for(j=2;s[j]!='\0';j++)  
                {
ma[i][s[j]-'A'+1] = ma[s[j]-'A'+1][i] = 1;
ans=0;
}
        }  
        color[1]=1;   
        if(ans==1)    cout<<"1 channel needed."<<endl; 
        else if(dfs(2,2))    cout<<"2 channels needed."<<endl; 
        else if(dfs(2,3))    cout<<"3 channels needed."<<endl;  
        else cout<<"4 channels needed."<<endl;;  
    }  
    return 0;  
}  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值