POJ 1129 Channel Allocation 四色定理dfs
题目: http://poj.org/problem?id=1129
开始没读懂题,看discuss的做法,都是循环枚举的,很麻烦。然后我就决定dfs,调试了半天终于0ms A了。
#include <stdio.h>
#include <string.h>
bool graph[][], vis[][];
int n, ans; void calc()
{
int cnt = ;
for(int i = ; i < ; i++)
{
for(int j = ; j < n; j++)
{
if(vis[j][i])
{
cnt++;
break;
}
}
}
if(cnt < ans)ans = cnt;
} void dfs(int x)
{
if(x >= n)
{
calc();
return;
} for(int i = ; i < ; i++)
{
bool ok = ;
for(int j = ; j < n; j++)
{
if((graph[j][x] && vis[j][i]) || (graph[x][j] && vis[j][i]))
{
ok = ;
break;
}
}
if(ok)
{
vis[x][i] = ;
dfs(x+);
vis[x][i] = ;
}
}
} int main()
{
char s[];
while(scanf("%d", &n) != EOF && n)
{
ans = 0x3f3f3f3f;
memset(graph, , sizeof(graph));
memset(vis, , sizeof(vis));
for(int i = ; i < n; i++)
{
scanf("%s", s);
for(int j = ; s[j]; j++)
{
graph[i][s[j]-'A'] = ;
}
}
vis[][] = ;
dfs();
if(ans == )
printf("1 channel needed.\n");
else printf("%d channels needed.\n", ans);
}
return ;
}
POJ 1129 Channel Allocation 四色定理dfs的更多相关文章
- POJ 1129 Channel Allocation(DFS)
Channel Allocation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 13173 Accepted: 67 ...
- 迭代加深搜索 POJ 1129 Channel Allocation
POJ 1129 Channel Allocation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14191 Acc ...
- POJ 1129 Channel Allocation DFS 回溯
Channel Allocation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 15546 Accepted: 78 ...
- poj 1129 Channel Allocation ( dfs )
题目:http://poj.org/problem?id=1129 题意:求最小m,使平面图能染成m色,相邻两块不同色由四色定理可知顶点最多需要4种颜色即可.我们于是从1开始试到3即可. #inclu ...
- 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
http://poj.org/problem?id=1129 import java.util.*; import java.math.*; public class Main { public st ...
- Channel Allocation(四色定理 dfs)
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 10897 Accepted: 5594 Description When ...
- POJ 1129:Channel Allocation 四色定理+暴力搜索
Channel Allocation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 13357 Accepted: 68 ...
- Channel Allocation(DFS)
Channel Allocation Time Limit : 2000/1000ms (Java/Other) Memory Limit : 20000/10000K (Java/Other) ...
随机推荐
- CameraTest
from com.android.monkeyrunner import MonkeyRunner,MonkeyDevice,MonkeyImage #device = MonkeyRunner.wa ...
- [React Native] Up and Running
We'll download the requirements for getting started with React Native, refactor our app to ES6, walk ...
- java13 InputStream,Reader
流的方向: .输入流:数据源到程序(InputStream,Reader读进来). .输出流:程序到目的地(OutPutStream,Writer写出来). 处理数据单元: 字节流:按照字节读取数据( ...
- C# - 集合类 - 集合接口
本篇将介绍关于集合的接口 这些接口定义了所有与集合有关的类的框架 IEnumerable接口 ns:System.Collections 此接口定义了对集合遍历的方法 一般表示元素序列或集合的类都实现 ...
- 摄像机(CCCamera)
- (转载)Ant教程
ant教程(一) 写在所有之前 为了减少阅读的厌烦,我会使用尽量少的文字,尽量明白的表达我的意思,尽我所能吧.作为一个学习者,在我的文章中会存在各种问题,希望热心人指正.目录大概是这样 ant教程 ( ...
- Topself
TopShelf简介 个人理解:开源.跨平台的服务框架.提供一种方式以控制台编写windows服务,与windows服务相比,目前只发现便于调试. 官网网站:http://docs.topshelf- ...
- .net的 async 和 await
async 和 await 出现在C# 5.0之后,关系是两兄弟,Task是父辈,Thread是爷爷辈,这就是.net 多线程处理的东西,具体包括 创建线程,线程结果返回,线程中止,线程中的异常处理 ...
- JS获取日期和时间
//获取日期和时间 function showDate(){ var myDate = new Date(); myDate.getYear(); //获取当前年份(2位) myDate.getFul ...
- ios UIWebview本地加载H5网页
注意两点 1.拖动文件到工程中选择create folder,文件夹为蓝色 --不要让文件参与编译,而只是让文件加入进来 2.加载方式pathforresorth oftype indire ...