poj 1129 搜索
Channel Allocation
Time Limit: 1000 MS Memory Limit: 10000 KB
64-bit integer IO format: %I64d , %I64u Java class name: Main
Description
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
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
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.
#include <iostream>
#include <string.h>
#include <stdio.h> using namespace std;
/*int n; ///n个广播站
bool map[35][35]; ///个广播站之间的联系关系
int ans; ///需要多少广播站
int color[35]; ///染色
bool IsFind;*/ int n;
bool IsFind;
int ans;
int color[];
bool map[][];
///两个版本定义有什么区别呢?????????????????? bool OK(int x,int c) ///判断相邻节点颜色是否相同
{
for(int i=;i<n;i++)
{
if(map[x][i]&&c==color[i]) ///id节点与各个节点比较 x与id节点有连边&&颜色重复了
{
return false;
}
}
return true;
} void DFS(int id,int total) ///当前染色节点编号 总共用的颜色数量
{
if(IsFind)
return ;
if(id>=n)
{
IsFind=true;
return ;
} for(int i=;i<=total;i++)
{
if(OK(id,i))
{
color[id]=i; ///符合条件就上色 之前的颜色
DFS(id+,total); ///继续加点
color[id]=; ///回溯
}
}
if(!IsFind) ///之前的颜色都不符合要求
{
ans++; ///加点
DFS(id,total+); ///加颜色
}
} int main()
{
char str[];
while(scanf("%d",&n)!=EOF)
{ if(n==)
break;
memset(map,false,sizeof(map));
memset(color,,sizeof(color));
for(int i=;i<=n;i++)
{
cin>>str;
int len=strlen(str);
for(int j=;j<=len;j++)
{
map[str[]-'A'][str[j]-'A']=true;;
}
}
IsFind=false;
ans=;
DFS(,);
if(ans==)
printf("1 channel needed.\n");
else
printf("%d channels needed.\n",ans);
}
return ;
}
poj 1129 搜索的更多相关文章
- 迭代加深搜索 POJ 1129 Channel Allocation
POJ 1129 Channel Allocation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14191 Acc ...
- catch that cow POJ 3278 搜索
catch that cow POJ 3278 搜索 题意 原题链接 john想要抓到那只牛,John和牛的位置在数轴上表示为n和k,john有三种移动方式:1. 向前移动一个单位,2. 向后移动一个 ...
- POJ 1129:Channel Allocation 四色定理+暴力搜索
Channel Allocation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 13357 Accepted: 68 ...
- poj 1129 Channel Allocation ( dfs )
题目:http://poj.org/problem?id=1129 题意:求最小m,使平面图能染成m色,相邻两块不同色由四色定理可知顶点最多需要4种颜色即可.我们于是从1开始试到3即可. #inclu ...
- POJ 1129 Channel Allocation 四色定理dfs
题目: http://poj.org/problem?id=1129 开始没读懂题,看discuss的做法,都是循环枚举的,很麻烦.然后我就决定dfs,调试了半天终于0ms A了. #include ...
- poj 1129 Channel Allocation
http://poj.org/problem?id=1129 import java.util.*; import java.math.*; public class Main { public st ...
- poj 1129(dfs+图的四色定理)
题目链接:http://poj.org/problem?id=1129 思路:根据图的四色定理,最多四种颜色就能满足题意,使得相邻的两部分颜色不同.而最多又只有26个点,因此直接dfs即可. #inc ...
- [Vjudge][POJ][Tony100K]搜索基础练习 - 全题解
目录 POJ 1426 POJ 1321 POJ 2718 POJ 3414 POJ 1416 POJ 2362 POJ 3126 POJ 3009 个人整了一些搜索的简单题目,大家可以clone来练 ...
- poj 2251 搜索
Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13923 Accepted: 5424 D ...
随机推荐
- XStream将XML转javaben,出现多余的tag,导致出错
今天在测试银联无卡快捷支付的案例时,多了一个多tag兼容性测试,它是指银联的XML报文中会出现多余的tag,如果我们用XStream解析的时候,没有Javabean的字段可以对应上,就会报错!提示: ...
- BZOJ1088或洛谷2327 [SCOI2005]扫雷
BZOJ原题链接 洛谷原题链接 很容易发现答案就只有\(0,1,2\)三种答案,而且只要知道第一个格子是否有雷就可以直接顺推下去了. 所以我们跑一次首位有雷,跑一次首位无雷判断是否可行即可. #inc ...
- BZOJ3191或洛谷2059 [JLOI2013]卡牌游戏
BZOJ原题链接 洛谷原题链接 我们可以倒着来\(DP\). 设\(f[i][j]\)表示剩余\(i\)个人,从庄家数起第\(j\)个人的胜率,设当前枚举到第\(k\)张牌,该情况下这一轮淘汰的位置为 ...
- C++动态链接库
1.动态链接库概述: 动态链接库通常都不能直接运行,也不能接受消息:只有在其他模块调用动态链接库中的函数时,它才发挥作用. Windows API中所有的函数都包含在动态链接库中. 动态链接库分静态库 ...
- error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int 错误的解决方法
一些函数定义中,在VC6中,如果没有显示的指定返回值类型,编译器将其视为默认整型:但是vs2008/vs2010不支持默认整型,解决这个问题不能修改每个没有显示指示返回值类型的函数地方,可以用排除44 ...
- 网页定时器setTimeout( )
不斷重複執行的 setTimeout( ) setTimeout( ) 預設只是執行一次, 但我們可以使用一個循環方式, 使到一個setTimeout( ) 再啟動自己一次, 就會使到第二個 setT ...
- zabbix监控系统_监控收集脚本使用分享
性能测试总是要监控服务器,做了zabbix监控之后,重要收集监控数据,这里分享下我是怎么做的. 准备文件 python2.7 pypa-setuptools.tar.gz -p -path /h ...
- django数据库多对多修改对应关系
obj = models.Book.objects.get(id=1) #添加对应关系,给书添加作者# obj.m.add(3)# obj.m.add(2,4)# obj.m.add(*[1,2,3, ...
- Java SE学习【二】——面向对象
面向对象的学习也进行了一段时间,这段时间学了,类和对象:属性:方法:封装:继承:多态:接口.也算是有一些自己的理解,不愧是贴近人类思维的思想,老师讲时我常常会想到以前的一些事物和其交相印证,其中最常想 ...
- webapp利用iscroll实现同时横滚|竖滚
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...