zoukankan      html  css  js  c++  java
  • 深搜,四色定理,(POJ1129)

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

    解题报告:

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    
    bool map[33][33];
    int mark[33];
    char str[33];
    int n,ans;
    
    ///判断给x这个节点,上color这种颜色是否可以
    bool Judge(int x,int color)
    {
        for(int i=0; i<n; i++)
        {
            if(i!=x&&map[x][i]&&mark[i]==color)///如果子节点同色则不可以
                return 0;
        }
        return 1;
    }
    
    ///搜索第pos个节点
    void dfs(int pos)
    {
        ///搜索完毕。
        if(pos==n)
        {
            ///*max_element(mark,mark+n);是查找[mark,mark+n)中的最大者
            ans=min(ans,*max_element(mark,mark+n));
            return ;
        }
        ///从pos这个节点开始搜起
        for(int i=pos; i<n; i++)
        {
            ///四色定理
            for(int j=1; j<=4; j++)
            {
                ///给i这个节点上j这种颜色是否符合
                if(Judge(i,j))
                {
                    ///符合题意的话,给i上j这种颜色
                    mark[i]=j;
                    ///搜索下一个节点
                    dfs(i+1);
                }
            }
        }
    }
    
    
    int main()
    {
        while(scanf("%d",&n),n)
        {
            memset(map,false,sizeof(map));
            for(int i=0; i<n; i++)
            {
                scanf("%s",str);
                for(int j=2; j<strlen(str); j++)
                {
                    map[i][str[j]-'A']=true;
                    map[str[j]-'A'][i]=true;
                }
            }
            memset(mark,0,sizeof(mark));
            ans=4;
            mark[0]=1;
            dfs(1);
            if(ans==1)
            {
                printf("1 channel needed.
    ");
            }
            else
                printf("%d channels needed.
    ",ans);
        }
        return 0;
    }
  • 相关阅读:
    MD5双重加密设计
    ComBox(自定义封装)LimitToList属性和做到移走光标不是下拉项清空输入
    强制下线功能
    广播
    动态添加碎片
    RecyclerView
    Listview的运行效率
    Listview
    通知栏
    补间动画
  • 原文地址:https://www.cnblogs.com/TreeDream/p/5364382.html
Copyright © 2011-2022 走看看