HDU 1373 Channel Allocation

题目地址:点击打开链接

思路:四色问题,最多4种颜色即可,尝试每种颜色,当需要数大于1时,channel要加s

AC代码:

#include<iostream>
#include<cstring>

using namespace std;

int g[30][30];
int used[30];
int n;

bool dfs(int id,int color)
{
    int i,j;
    bool flag;
    for(i=0; i<color; i++)
    {
        flag = true;
        used[id] = i;
        for(j=0; j<id; j++)
        {
            if(g[id][j] && used[j] == used[id])
            {
                flag =false;
                break;
            }
        }
        if(flag && (id == n-1 || dfs(id+1,color)))
            return true;
    }
    return false;
}
int main()
{
    char sport[50];
    int i,j,one;
    while(cin>>n && n)
    {
        one = 1;
        //memset(used,0,sizeof(used));//used不用初始化也可,程序里面会覆盖
        memset(g,0,sizeof(g));
        for(i=0; i<n; i++)
        {
            cin>>sport;
            for(j=2; sport[j]; j++,one=0)
            {
                g[i][sport[j] - 'A'] = 1;
                g[sport[j] - 'A'][i] = 1;
            }
        }
        if(one)
            cout<<"1 channel needed."<<endl;
        else if(dfs(0,2))//也可从1搞,默认第0个用第0种颜色
            cout<<"2 channels needed."<<endl;
        else if(dfs(0,3))
            cout<<"3 channels needed."<<endl;
        else
            cout<<"4 channels needed."<<endl;
    }
    return 0;
}


你可能感兴趣的:(HDU 1373 Channel Allocation)