您的位置:首页 > 其它

POJ 1129 Channel Allocation(图的着色问题)

2016-05-02 15:02 399 查看
题目地址:http://poj.org/problem?id=1129

题意:相当于地图的着问题,临近的国家要用不同的颜色去涂,最多只需要4中颜色即可

思路:枚举颜色即可

AC代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <cstring>
#include <climits>
#include <cmath>
#include <cctype>
const int inf = 0x3f3f3f3f;//1061109567
typedef long long LL;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
char a[30];
int map1[30][30];
int used[30];
int n;
bool dfs(int cur,int color)
{
for(int i=0; i<color; i++)
{
bool flag = true;
used[cur] = i;
for(int j=0; j<cur; j++)
{
if(map1[j][cur] && used[j] == used[cur])
{
flag = false;
break;
}
}
if(flag && (cur == n-1 || dfs(cur+1,color)))
return true;
}
return false;//都不行
}
int main()
{
while(scanf("%d",&n) && n)
{
bool flag = true;
memset(map1,0,sizeof(map1));
for(int i=0; i<n; i++)
{
scanf("%s",a);
int len = strlen(a);
for(int j=2; j<len; j++)
{
flag = false;
map1[i][a[j]-'A'] = map1[a[j]-'A'][i] = 1;
}
}
if(flag)
printf("1 channel needed.\n");
else if(dfs(0,2))//别人这的第一个参数是从1开始,不太了解
printf("2 channels needed.\n");
else if(dfs(0,3))
printf("3 channels needed.\n");
else
printf("4 channels needed.\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: