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;
}
题意:相当于地图的着问题,临近的国家要用不同的颜色去涂,最多只需要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;
}
相关文章推荐
- 高性能的通讯库-zeroMQ的几个高性能特征
- Web本地存储 - localStorage
- Linux学习笔记:常用100条命令(二)
- CentOS 7 安装virtualBox
- 男子给妻子做了张桌子,他病逝后家人偶然发现...
- redis 一二事 - 设置过期时间,以文件夹形式展示key显示缓存数据
- Java本地文件操作(五)遍历文件夹
- 算法练习2
- Ajax
- 【补充习题四】凑微分技巧与积分因子法解常微分方程
- scoped_array
- C++ 重载解析 知多少?
- UIKit Dynamics动力系统入门
- 设置<a>元素的背景图像
- HDU-4850 Wow! Such String! (构造)
- 动画
- 事件
- fork()函数
- Linux学习笔记:常用100条命令(一)
- 核心板产品