POJ 1129 Channel Allocation【四色猜想+DFS染色】


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 strong signal. However, the channels used by each repeater must be carefully chosen so that nearby repeaters do not interfere with one another. This condition is satisfied if adjacent repeaters use different channels. 

Since the radio frequency spectrum is a precious resource, the number of channels required by a given network of repeaters should be minimised. You have to write a program that reads in a description of a repeater network and determines the minimum number of channels required.

Input

The input consists of a number of maps of repeater networks. Each map begins with a line containing the number of repeaters. This is between 1 and 26, and the repeaters are referred to by consecutive upper-case letters of the alphabet starting with A. For example, ten repeaters would have the names A,B,C,...,I and J. A network with zero repeaters indicates the end of input. 

Following the number of repeaters is a list of adjacency relationships. Each line has the form: 

A:BCDH 

which indicates that the repeaters B, C, D and H are adjacent to the repeater A. The first line describes those adjacent to repeater A, the second those adjacent to B, and so on for all of the repeaters. If a repeater is not adjacent to any other, its line has the form 

A: 

The repeaters are listed in alphabetical order. 

Note that the adjacency is a symmetric relationship; if A is adjacent to B, then B is necessarily adjacent to A. Also, since the repeaters lie in a plane, the graph formed by connecting adjacent repeaters does not have any line segments that cross. 

Output

For each map (except the final one with no repeaters), print a line containing the minumum number of channels needed so that no adjacent channels interfere. The sample output shows the format of this line. Take care that channels is in the singular form when only one channel is required.

Sample Input

2
A:
B:
4
A:BC
B:ACD
C:ABD
D:BC
4
A:BCD
B:ACD
C:ABD
D:ABC
0

Sample Output

1 channel needed.
3 channels needed.
4 channels needed. 

题意&思路:

n个字母,每个字母对应一个区域,相邻的区域之间的颜色不能相同,这里要了解一个叫四色猜想的知识,可以去百度了解一下,大致就是说,一个地图,地图内有无数个不规则的区域,对这些区域进行染色,无论区域有多少个,最多用4种颜色就能够完成条件的要求进行染色,用dfs来暴力枚举每一种颜色,记录所用颜色最少的数,这里其实并不用太在意这个没有一个相邻的字母,仔细想想,如果没有与这个字母相邻的话,其实任何一个颜色都符合,在不考虑这些没有相邻的字母的情况下去算出需要的最少颜色数量,如果这个数量不为 0 ,那么 ,就不用管这个没有相邻的字母,因为随便给一个颜色都可以满足条件,如果这个数量为0,那么,输出最少颜色数量为 1 就行了,因为有相邻的所需颜色为0,那只需要给一个颜色给没有相邻的就行了;下面给出代码:


#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<string>
#include<map>
using namespace std;

int mp[30][30], n, res, vis[30], cnt;
char fm[30];


// 判断这个颜色会不会与相邻的区域颜色相同
bool cheak(int cur, int clour) {
	for (int i = 1; i <= n; ++i) {
		if (mp[cur][i] == 1) {
			if (vis[i] == clour) return false;
		}
	}
	return true;
}

void dfs(int cur_p) {  // 1,0,1,1
	int tp, temp; 
	// 这里染色是边访问一个位置边染色,用vis来记录颜色,同时不为 0 说明已经访问过了
	if (cur_p == n + 1) { // 当染色完最后一个区域就结束,然后溯回
		temp = vis[1];
		for (int k = 2; k <= n; ++k) if (temp < vis[k]) temp = vis[k];
		if (res > temp) res = temp;
		return;
	}
	for (int i = 1; i <= 4; ++i) { // 四种颜色遍历
		if (cheak(cur_p, i) == true) {
			tp = vis[i];
			vis[cur_p] = i;
			dfs(cur_p + 1);  
			vis[cur_p] = tp;
		}
	}
	return;
}


int main(void)
{
	int len;
	while (scanf("%d", &n) != EOF) {
		if (n == 0) break;
		getchar();
		memset(mp, 0, sizeof(mp));
		int flag = 0, temp;
		for (int i = 0; i < n; ++i) {
			scanf("%s", fm);
			getchar();
			len = strlen(fm);
			if (len == 2) {
				flag = 1;
				continue;
			}
			else {
				int tmp = fm[0] - 'A' + 1;
				for (int j = 2; j < len; ++j) {
					temp = fm[j] - 'A' + 1;
					mp[tmp][temp] = 1;
				}
			}
		}
		memset(vis, 0, sizeof(vis));
		res = 9999999;
		dfs(1);
		// 如果 res 的值不变,那么说明有相邻区域所需颜色数量为0,那么输出 1 就行了
		if (res == 1 || res == 9999999) printf("1 channel needed.\n");
		else printf("%d channels needed.\n", res);
	}
	return 0;
}





猜你喜欢

转载自blog.csdn.net/godleaf/article/details/80465219