【POJ】1129【涂色问题】【Channel Allocation】

为啥人家都是用DFS做的呢?我就是用灰常普通的做法做滴:

原文链接:http://poj.org/problem?id=1129

//大致意思就是用最小的颜色涂满所有区域,且相邻区域不使用同种颜色(这里的相邻就是例如A:BC,A与B、C相邻)
 

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.
#include <stdio.h>
#include <string.h>
#define INF 30

int min=INF,n,mi,sum;
int map[26][26];
int color[26];
void play(int flag){//flag为0意味着上一次有点的color值发生变化,有需要重新找新的点
    if(!flag){
        int k,i;
        flag=1;
        sum++;
        for(k=0; k<n; k++){
            if(color[k]==sum){
                for(i=k+1; i<n; i++){
                    if(map[k][i] && color[k]==color[i]){ //相连&&颜色相同就要改变
                        flag=0;
                        color[i]++;//因为与k点的color值相同,因此要发生改变,color值++
                    }
                }
            }
        }
        play(flag);
    }
    else
        return ;
}
int main(){
    while(scanf("%d",&n),n){
        int i;
        char a,b;
        memset(map,0,sizeof(map));
        memset(color,0,sizeof(color));
        getchar();
        for(i=0; i<n; i++){
            scanf("%c",&a);
            a-=65;
            getchar();
            while((b=getchar())!='\n'){
                b-=65;
                map[a][b]=1;
            }
            color[a]++;//每点的color初值均为1
        }
        sum=0;
        play(0);
        if(sum==1)//只有一种color值
            printf("1 channel needed.\n");
        else
            printf("%d channels needed.\n",sum);
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值