四色定理+dfs(poj 1129)
题意:要求A:BCD,A与B,C,D都不相同,求不同的值,典型的四色定理;
- #include <iostream>
- #include <algorithm>
- #include <stdlib.h>
- #include <time.h>
- #include <cmath>
- #include <cstdio>
- #include <string>
- #include <cstring>
- #include <vector>
- #include <queue>
- #include <stack>
- #include <set>
- #define c_false ios_base::sync_with_stdio(false); cin.tie(0)
- #define INF 0x3f3f3f3f
- #define INFL 0x3f3f3f3f3f3f3f3f
- #define zero_(x,y) memset(x , y , sizeof(x))
- #define zero(x) memset(x , 0 , sizeof(x))
- #define MAX(x) memset(x , 0x3f ,sizeof(x))
- #define swa(x,y) {LL s;s=x;x=y;y=s;}
- using namespace std ;
- #define N 105
- const double PI = acos(-1.0);
- typedef long long LL ;
- int mapp[N][N], color[N];
- int col,flag;
- int n;
- string s;
- bool ok(int i){
- for(int j = ; j <= ; j++){
- if(!mapp[i][j]) continue;
- if(color[i] == color[j]) return false;
- }
- return true;
- }
- void dfs(int num){
- if(num > n) {flag = ; return ;}
- for(int i = ; i <= col; i ++){
- color[num] = i;
- if(ok(num))
- dfs(num+);
- color[num]= ; ///记得回溯时要清零啊!!!
- }
- }
- int main(){
- //freopen("in.txt","r",stdin);
- //freopen("out.txt","w",stdout);
- while(cin>>n && n){
- flag = ;
- zero(mapp);
- for(int i = ; i< n;i++){
- cin>>s;
- int k = s.size();
- int x,y;
- for(int j = ;j< k;j++){
- x = s[] - 'A' +;
- y = s[j] - 'A' +;
- mapp[x][y] = mapp[y][x] = ;
- }
- }
- for(col = ; col <= ; col++){
- dfs();
- if(flag) break;
- }
- if(col == )
- printf("%d channel needed.\n", col);
- else
- printf("%d channels needed.\n", col);
- }
- return ;
- }
四色定理+dfs(poj 1129)的更多相关文章
- 迭代加深搜索 POJ 1129 Channel Allocation
POJ 1129 Channel Allocation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14191 Acc ...
- poj 1129(dfs+图的四色定理)
题目链接:http://poj.org/problem?id=1129 思路:根据图的四色定理,最多四种颜色就能满足题意,使得相邻的两部分颜色不同.而最多又只有26个点,因此直接dfs即可. #inc ...
- POJ 1129 Channel Allocation 四色定理dfs
题目: http://poj.org/problem?id=1129 开始没读懂题,看discuss的做法,都是循环枚举的,很麻烦.然后我就决定dfs,调试了半天终于0ms A了. #include ...
- poj 1129 Channel Allocation ( dfs )
题目:http://poj.org/problem?id=1129 题意:求最小m,使平面图能染成m色,相邻两块不同色由四色定理可知顶点最多需要4种颜色即可.我们于是从1开始试到3即可. #inclu ...
- POJ 1129:Channel Allocation 四色定理+暴力搜索
Channel Allocation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 13357 Accepted: 68 ...
- Channel Allocation (poj 1129 dfs)
Language: Default Channel Allocation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12 ...
- POJ 1129 Channel Allocation(DFS)
Channel Allocation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 13173 Accepted: 67 ...
- poj 1129 Channel Allocation(图着色,DFS)
题意: N个中继站,相邻的中继站频道不得相同,问最少需要几个频道. 输入输出: Sample Input 2 A: B: 4 A:BC B:ACD C:ABD D:BC 4 A:BCD B:ACD C ...
- POJ 1129 Channel Allocation DFS 回溯
Channel Allocation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 15546 Accepted: 78 ...
随机推荐
- 16. Copy List with Random Pointer
类同:剑指 Offer 题目汇总索引第26题 Copy List with Random Pointer A linked list is given such that each node cont ...
- Windows 2008等操作系统升级时出现800F0818错误代码的解决方法
今天我在网络中的一台Windows Server 2008 R2升级时,出现“代码800F0818”的错误提示,如图1-1所示.
- js实现右侧的分享效果
就是当鼠标移到上面的时候,他会出现,鼠标移出就消失. nmouseover和nmouseout就能实现 <style> *{padding:0;margin:0;} #s1{width:2 ...
- DataSet读取XML
string file = File.ReadAllText("c://123.xml", Encoding.Default); using (DataSet ds = new D ...
- python笔记一
好奇,想一探究竟.安装就出点小问题,win7,64位,一直卡在这里不动了? 只好取消.第二天安装仍是如此. 于是下载Windows6.1-KB2999226-x64.msu,安装,仍卡顿不动: 于是找 ...
- python迭代器与生成器
1.迭代器 iterator 迭代器是一种对象类型!可以由生成器生成! 迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问完结束.迭代器的一大优点是不要求事先准备好整个迭代过程中所有的元素.迭 ...
- PHPDocument 代码注释规范总结
PHPDocument 代码注释规范 1. 安装phpDocumentor(不推荐命令行安装)在http://manual.phpdoc.org/下载最新版本的PhpDoc放在web服务器目录下使得通 ...
- APP测试工具之TraceView卡顿检测
Traceview卡顿检测 Traceview是Android平台特有的数据采集和分析工具,集成在DDMS工具中,可以采集程序中的方法执行耗时.调用关系.调用次数以及资源占用等情况. 一.使用方法 1 ...
- 【转载】OpenGL超级宝典笔记——GLSL语言基础
变量 GLSL的变量命名方式与C语言类似.变量的名称可以使用字母,数字以及下划线,但变量名不能以数字开头,还有变量名不能以gl_作为前缀,这个是GLSL保留的前缀,用于GLSL的内部变量.当然还有一些 ...
- socket编程相关的结构体和字节序转换、IP、PORT转换函数
注意:结构体之间不能直接进行强制转换, 必须先转换成指针类型才可以进行结构体间的类型转换, 这里需要明确的定义就是什么才叫强制转换. 强制转换是将内存中一段代码以另一种不同类型的方式进行解读, 因此转 ...