poj 1129 Channel Allocation (dfs)

链接:poj 1129

题意:如果相邻的中继器使用不同的频道,就不会相互干扰。

给定一些中继器的相邻关系,问至少要选几个不同的频道,使得中继器都不互相干扰。

分析:这题可以转化为无向图的染色问题,

即相邻的点不能染同一种颜色,求至少需要的几种颜色?

本题顶点数最多为26,可以直接用暴力搜索即可

思路:若顶点总数为n,则最多需要n种颜色(编号为1,2...n),

从最小的顶点开始染色,每次将与该顶点相邻的已染色的颜色标记,

再从未标记(未用)的颜色中,选出一个最小的颜色,给该点染色,

所有点染色完后,再统计用了几种颜色就是所求答案.

注:如果答案为1时,输出的channel为单数,否则为复数channels

#include<stdio.h>
#include<string.h>
int main()
{
    int n,i,j,vis[30],color[30],sum;
    char s[30];
    while(scanf("%d",&n)!=EOF){
        if(n==0)
            break;
        memset(color,0,sizeof(color));
        for(i=0;i<n;i++){
            scanf("%s",s);
            memset(vis,0,sizeof(vis));
            for(j=2;s[j]!='\0';j++)  //标记相邻顶点所染的颜色
                if(color[s[j]-'A'])
                    vis[color[s[j]-'A']]=1;
            for(j=1;j<=n;j++)      //找到最小的未被用的颜色,给顶点i染色
                if(!vis[j]){
                    color[i]=j;  //给顶点i染j颜色
                    break;
                }
        }
        memset(vis,0,sizeof(vis));
        sum=0;
        for(i=0;i<n;i++){      //统计所用不同颜色的种数
            if(!vis[color[i]]){
                sum++;
                vis[color[i]]=1;
            }
        }
        if(sum==1)
            printf("%d channel needed.\n",sum);
        else
            printf("%d channels needed.\n",sum);
    }
    return 0;
}
时间: 2024-10-23 04:24:12

poj 1129 Channel Allocation (dfs)的相关文章

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

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

POJ 1129 Channel Allocation(暴力搜--涂色问题)

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

DFS/四色定理/poj 1129 Channel Allocation

1 #include<cstdio> 2 #include<cstring> 3 using namespace std; 4 int n; 5 int a[30][30]; 6 int c[30]; 7 8 bool pd(int x,int color) 9 { 10 for (int i=1;i<x;i++) 11 if (a[x][i]==1 && c[i]==color) return false; 12 return true; 13 } 14 1

poj 1129 Channel Allocation(四色定理)

1 #include<iostream> 2 #include<cstdio> 3 #include<cstdlib> 4 #include<cstring> 5 #include<string> 6 #include<queue> 7 #include<algorithm> 8 #include<map> 9 #include<iomanip> 10 #include<climits>

poj1129 Channel Allocation DFS

题目 题意:给定N个节点,让你对其涂色,使其任何相邻的两个节点颜色不同. 思路: 1. 问题模型是着色问题,枚举颜色的个数, 每次枚举看可以完成全部点的着色. 2. 采用深搜,每一次当前色的种数深搜完毕就加1种,直到全部点都被着色完毕, 这样 从最少的开始深搜,结果出现肯定是最少的. 该题N<26,可以不用四色原理剪枝. 附上一发代码: 1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #i

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

四色定理+dfs(poj 1129)

题目:Channel Allocation 题意:要求A:BCD,A与B,C,D都不相同,求不同的值,典型的四色定理: #include <iostream> #include <algorithm> #include <stdlib.h> #include <time.h> #include <cmath> #include <cstdio> #include <string> #include <cstring&