poj1129——Channel Allocation
来源:互联网 发布:声音剪辑软件 编辑:程序博客网 时间:2024/05/16 08:05
题目大意:相邻中继器不能设置相同频道,否则会干扰信号,问在这个中继器网络中最少设置多少个频道
输入:(有很多case,输入以0结束)
第i个case中地图包含的中继器个数n(1<=n<=26)
第k个中继器的序号:与它相邻的站点序号(序号用大写字母表示,如果没有相邻站点则输入为A:)
输出:? channel(s) needed(注意如果?为1则channel为单数形式)
分析:dfs搜索,其实本题就是一个点着色问题,相邻点必须着不同颜色
每个点的子结点就是它能选择的颜色,在当前这些种颜色下,如果对这个点dfs结束还无解,就加一种颜色重新对这个结点进行dfs
代码:转载自http://blog.csdn.net/non_cease/article/details/7313613
- #include <iostream>
- #include <cstdio>
- #include <cstring>
- using namespace std;
- #define M 26
- int n, ans, color[M];
- bool map[M][M], isFind;
- bool ok(int x, int c) { //判断是否存在相邻节点颜色相同
- for (int i = 0; i < n; i++)
- if (map[x][i] && c == color[i])
- return false;
- return true;
- }
- void DFS(int id, int total) { //当前搜索到下标为id的节点,此时总共用的色彩数为total
- if (isFind) return;
- if (id >= n) { isFind = true; return; } //当所有节点都被着色后,返回
- for (int i = 1; i <= total; i++) {
- if (ok(id, i)) {
- color[id] = i;
- DFS(id+1, total);
- color[id] = 0;
- }
- }
- if (!isFind) { //当用total种颜色无法完成时,则增加一种颜色进行着色
- ans++;
- DFS(id, total+1);
- }
- }
- int main()
- {
- int i, j;
- char s[M];
- while (scanf ("%d", &n) && n) {
- getchar();
- memset (map, false, sizeof (map));
- memset (color, 0, sizeof (color));
- for (i = 0; i < n; i++) {
- gets(s);
- for (j = 2; s[j] != '\0'; j++)
- map[s[0]-'A'][s[j]-'A'] = true;
- }
- isFind = false;
- ans = 1;
- DFS(0, 1);
- if (ans == 1)
- printf ("1 channel needed.\n");
- else printf ("%d channels needed.\n", ans);
- }
- return 0;
- }
阅读全文
0 0
- POJ1129—Channel Allocation
- POJ1129——Channel Allocation
- poj1129——Channel Allocation
- poj1129——Channel Allocation(dfs)
- POJ1129 Channel Allocation——染色问题 暴搜
- POJ1129--Channel Allocation
- POJ1129 Channel Allocation
- poj1129 Channel Allocation
- POJ1129 Channel Allocation
- POJ1129-Channel Allocation
- POJ1129 Channel Allocation DFS
- POJ1129 Channel Allocation
- POJ1129 Channel Allocation(AC)
- poj1129 Channel Allocation
- poj1129 Channel Allocation
- POJ1129-Channel Allocation
- HDU1373&&POJ1129-Channel Allocation
- POJ1129 Channel Allocation
- 初识实时流处理
- Mybatis plus like查询问题
- python 回调函数
- Mybatis 使用动态语句实现批量删除(delete结合foreach)
- 关于js动态渲染的元素绑定事件遇到的坑
- poj1129——Channel Allocation
- Kruskal模板 最小生成树
- 用户输入日、月和年(月份可以是月份号、月份名或月份缩写),程序返回一年中到给定日子(包括这一天)的总天数
- 如何成为一个架构师
- 图形学opengl实验一-渐变色六芒星
- springboot 集成pagehelper
- JDBC-ODBC桥接数据库
- 【吴恩达机器学习笔记学习笔记02】Cost function
- 架构师已死