POJ 1129-Channel Allocation(四色定理+迭代深搜)

题目链接:传送门

题意:n个信号站,给出连接情况,要用信号覆盖所有信号站,要求相连的信号站不能用同一个信号。

等价问题==无向图染色==四色定理(每个平面地图都可以只用四种颜色来染色,而且没有两个邻接的区域颜色相同。已证明)

思路:深搜一条路(枚举颜色,判断当前点用已有的颜色能不能染,如不能则加一种颜色,判断强判就行了),搜到头答案就出来了。。然后返回就可以了

注意单复数。。

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cctype>
#include <vector>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#define maxn 360
#define _ll __int64
#define ll long long
#define INF 0x3f3f3f3f
#define Mod 1000000007
#define pp pair<int,int>
#define ull unsigned long long
#define max(x,y) ( ((x) > (y)) ? (x) : (y) )
#define min(x,y) ( ((x) > (y)) ? (y) : (x) )
using namespace std;
int n, ans, ok, vis[28];
bool ma[28][28];
bool check(int u, int sb)
{
	for (int i = 1; i <= n; i++)
		if (ma[u][i] && vis[i] == sb) {
			return 0;
		}

	return 1;
}
void dfs(int u, int s)
{

	if (ok) {
		return ;
	}

	if (u == n + 1) {
		ans = s;
		ok = 1;
		return ;
	}

	for (int k = 1; k <= s; k++) {
		if (check(u, k)) {
			vis[u] = k;
			dfs(u + 1, s);
		}
	}

	vis[u] = ++s;
	dfs(u + 1, s);
}
int main()
{
	char s[38];

	while (scanf("%d", &n) != EOF && n) {
		memset(ma, 0, sizeof(ma));
		memset(vis, 0, sizeof(vis));
		getchar();

		for (int i = 1; i <= n; i++) {
			scanf("%s", s);
			int len = strlen(s);

			for (int j = 2; j <= len - 1; j++) {
				ma[i][s[j] - 'A' + 1] = 1;
				ma[s[j] - 'A' + 1][i] = 1;
			}
		}

		ok = 0;
		dfs(1, 1);

		if (ans == 1) {
			puts("1 channel needed.");
		} else {
			printf("%d channels needed.\n", ans);
		}
	}

	return 0;
}
时间: 2024-10-25 20:39:11

POJ 1129-Channel Allocation(四色定理+迭代深搜)的相关文章

poj 1129 -- Channel Allocation

Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12143   Accepted: 6218 Description When a radio station is broadcasting over a very large area, repeaters are used to retransmit the signal so that every receiver has a s

POJ 1129 Channel Allocation(暴力搜--涂色问题)

Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13295   Accepted: 6806 Description When a radio station is broadcasting over a very large area, repeaters are used to retransmit the signal so that every receiver has a s

poj 1129 Channel Allocation (dfs)

链接:poj 1129 题意:如果相邻的中继器使用不同的频道,就不会相互干扰. 给定一些中继器的相邻关系,问至少要选几个不同的频道,使得中继器都不互相干扰. 分析:这题可以转化为无向图的染色问题, 即相邻的点不能染同一种颜色,求至少需要的几种颜色? 本题顶点数最多为26,可以直接用暴力搜索即可 思路:若顶点总数为n,则最多需要n种颜色(编号为1,2...n), 从最小的顶点开始染色,每次将与该顶点相邻的已染色的颜色标记, 再从未标记(未用)的颜色中,选出一个最小的颜色,给该点染色, 所有点染色完

POJ 1129 Channel Allocation DFS 回溯

Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 15546   Accepted: 7871 Description When a radio station is broadcasting over a very large area, repeaters are used to retransmit the signal so that every receiver has a s

POJ 1129 Channel Allocation(DFS)

Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13173   Accepted: 6737 Description When a radio station is broadcasting over a very large area, repeaters are used to retransmit the signal so that every receiver has a s

poj 1129 Channel Allocation(四色定理)

1 #include<iostream> 2 #include<cstdio> 3 #include<cstdlib> 4 #include<cstring> 5 #include<string> 6 #include<queue> 7 #include<algorithm> 8 #include<map> 9 #include<iomanip> 10 #include<climits>

DFS/四色定理/poj 1129 Channel Allocation

1 #include<cstdio> 2 #include<cstring> 3 using namespace std; 4 int n; 5 int a[30][30]; 6 int c[30]; 7 8 bool pd(int x,int color) 9 { 10 for (int i=1;i<x;i++) 11 if (a[x][i]==1 && c[i]==color) return false; 12 return true; 13 } 14 1

poj 3134 Power Calculus iddfs(迭代深搜)

iddfs入门题. //poj 3134 //sep9 #include <iostream> using namespace std; int n,deep; int a[30]; bool iddfs(int pos) { int t; if(pos>deep) return false; if(a[pos]<<(deep-pos)<n) return false; if(a[pos]==n) return true; for(int i=1;i<=pos;+

UVA1374 - Power Calculus(迭代深搜+剪枝)

题目链接 题意:给出x和正整数n,问最少需要几次乘除法 可以得到n = x^m 思路:其实是关于指数的操作,即从1到m最少的步数.我们可以先确定最少步数m,然后进行迭代,迭代的过程也就是判断通过相加减所得到的数可以在m次操作中等于n,如果符合,m即为最小步数,如果不符合,m++,进行下一次迭代.迭代过程中要注意剪枝,即剩余的次数如果每次都是取最大值相加还是比n小的话,就直接跳出. 代码: #include <iostream> #include <cstdio> #include