zoukankan      html  css  js  c++  java
  • poj 1129(dfs+图的四色定理)

    题目链接:http://poj.org/problem?id=1129

    思路:根据图的四色定理,最多四种颜色就能满足题意,使得相邻的两部分颜色不同。而最多又只有26个点,因此直接dfs即可。

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 using namespace std;
     6 
     7 bool map[33][33];
     8 int mark[33];
     9 char str[33];
    10 int n,ans;
    11 
    12 bool Judge(int x,int color)
    13 {
    14     for(int i=0;i<n;i++){
    15         if(i!=x&&map[x][i]&&mark[i]==color)
    16             return 0;
    17     }
    18     return 1;
    19 }
    20 
    21 void dfs(int pos)
    22 {
    23     if(pos==n){
    24         ans=min(ans,*max_element(mark,mark+n));
    25         return ;
    26     }
    27     for(int i=pos;i<n;i++){
    28         for(int j=1;j<=4;j++){
    29             if(Judge(i,j)){
    30                 mark[i]=j;
    31                 dfs(i+1);
    32             }
    33         }
    34     }
    35 }
    36 
    37 
    38 int main()
    39 {
    40     while(~scanf("%d",&n)&&n){
    41         memset(map,false,sizeof(map));
    42         for(int i=0;i<n;i++){
    43             scanf("%s",str);
    44             for(int j=2;j<strlen(str);j++){
    45                 map[i][str[j]-'A']=true;
    46                 map[str[j]-'A'][i]=true;
    47             }
    48         }
    49         memset(mark,0,sizeof(mark));
    50         ans=4;
    51         mark[0]=1;
    52         dfs(1);
    53         if(ans==1){
    54             printf("1 channel needed.
    ");
    55         }else 
    56             printf("%d channels needed.
    ",ans);
    57     }
    58     return 0;
    59 }
    View Code
  • 相关阅读:
    Java实现 LeetCode 715 Range 模块(选范围)
    HTML 图像
    HTML 样式- CSS
    HTML <head>
    HTML 链接
    HTML 文本格式化
    目标检测中的anchor-based 和anchor free
    目标检测coco数据集点滴介绍
    Camera HDR Algorithms
    噪声标签的负训练:ICCV2019论文解析
  • 原文地址:https://www.cnblogs.com/wally/p/3295370.html
Copyright © 2011-2022 走看看