poj 1129 Channel Allocation
来源:互联网 发布:海康网络摄像头价位 编辑:程序博客网 时间:2024/05/22 14:15
题目链接:
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=98233#problem/G
题目大意:
平面内最多26个点,给出一些点与点之间的是否相连,问最少使用多少颜色才能给这些点染色,并保证相邻点之间不同色。根据图的四色定理,最多四种颜色就能满足题意。
分析:
其实用不着用dfs的,用两层循环就能搞定,从数组下标0到数组下标n-1代表每个点,遍历每个点,对其进行1~4的颜色选择,每次选择后与相邻点进行比较判断,如果有颜色相同,就不能选这个颜色,换一个颜色,如果符合,进行下一次循环。每次点的颜色选择从1开始。最后选出颜色数组中最大的值即为最少使用的颜色。
QAQ被这道题坑惨了,没注意到输出格式中的单复数形式,一个WrongAnswer。以为自己想错了,去网上一搜一片的DFS,心里拔凉拔凉的,完全不造错哪了。最后实在想不出来去看了一下别人的代码,DFS也是找到就退出递归就可以输出了,最后才反应过来是输出的问题。
代码:
#include<iostream>#include<stdio.h>#include<stdlib.h>#include<string.h>#include<algorithm>using namespace std;int map[30][30];int mark[30];int n;bool judge(int pos,int color){ for (int i = 0; i < n; i++) { if (map[pos][i]) { if (mark[i] == color) { return false; } } } return true;}int main(){ while (scanf("%d", &n) != EOF && n) { memset(map,0,sizeof(map)); memset(mark,0,sizeof(mark)); char str[35]; for (int i = 0; i<n; i++) { scanf("%s", str); int len = strlen(str); for (int j = 2; j<len; j++) { map[i][str[j] - 'A'] = 1; map[str[j] - 'A'][i] = 1; } } for (int i = 0; i <n ; i++) { for (int color = 1; color <= 4; color++) { mark[i] = color; if (judge(i, color)) { break; } } } int* min = (int*)max_element(mark, mark + n); if (*min == 1) { printf("%d channel needed.\n",*min); } else { printf("%d channels needed.\n",*min); } } return 0;}
0 0
- poj 1129 Channel Allocation
- poj 1129 Channel Allocation
- poj 1129 Channel Allocation
- poj 1129Channel Allocation
- poj 1129 Channel Allocation
- poj 1129 Channel Allocation
- POJ 1129 Channel Allocation
- POJ 1129 Channel Allocation
- POJ 1129 Channel Allocation
- POJ 1129 Channel Allocation
- POJ 1129 Channel Allocation
- poj 1129 Channel Allocation
- POJ 1129 Channel Allocation
- Channel Allocation(poj 1129 )
- poj 1129 Channel Allocation
- POJ 1129--Channel Allocation
- poj 1129 Channel Allocation
- POJ 1129 Channel Allocation
- 【Android】一步一步教你从Eclipse移植项目到Android Studio(1.4)
- Node.JS 简单爬虫
- spring定时任务@schedule
- pc端自适应测试
- linux head 命令
- poj 1129 Channel Allocation
- 总结@Autowired 和@Resource,对比异同点!
- NSString(拓展)
- Java中获得线程的返回值
- Linux内核代码风格
- 代码整洁之道
- SDUT 3362-数据结构实验之图论六:村村通公路(最小生成树_prim)
- 机器学习算法mini版
- js 运算误差解决方案