http://wikioi.com/problem/1116/

典型的DFS。

#include <iostream>
#include <memory.h> #define LEN 8
using namespace std; int graph[LEN][LEN];
int color[LEN]; // 1,2,3,4 for 4 colors
int ans = 0;
int n = 0; void dfs(int step) {
if (step == n) {
ans++;
return;
}
for (int x = 1; x <= 4; x++) {
bool valid = true;
for (int i = 0; i < step; i++) {
if (graph[i][step] == 1 && color[i] == x) {
valid = false;
break;
}
}
if (valid) {
color[step] = x;
dfs(step+1);
color[step] = 0;
}
}
} int main()
{
memset(graph, 0, sizeof(graph));
memset(color, 0, sizeof(color));
cin >> n;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> graph[i][j];
}
}
dfs(0);
cout << ans << endl;
return 0;
}

  

[wikioi]四色问题的更多相关文章

  1. 【wikioi】1116 四色问题

    题目链接 算法:DFS 刚开始卡了一下,但后面想了想,于是 放上代码: #include <iostream> using namespace std; bool map[9][9]; i ...

  2. 【wikioi】1041 Car的旅行路线

    题目链接 算法:最短路(数据弱,Floyd也能过) 惨痛的教训:此题我至少提交了20次,原因在于= =太草率和粗心了,看到那个多少组数据以为是城市的数量,导致数组开得小小的= =.(对不起,wikio ...

  3. 【wikioi】1040 统计单词个数

    题目链接 算法:划分型DP PS:被卡过3天.日期:2013-10-10 ~ 2013-10-12 18:52:48 这题是我提交了13次AC= =汗= = 题目描述: 给出一个长度不超过200的由小 ...

  4. [wikioi 1418]铃仙•优昙华院稻叶(东方幻想乡系列模拟赛)(树上递推)

    题目:http://www.wikioi.com/problem/1418/ 分析: 一看就肯定是树上的递推 设f[i][j][k]表示第i秒在k点(从j点走过来的)的概率 则f[i][j][k]=f ...

  5. [wikioi 1307][poj 2054]欧少堆(乱搞)

    题目:http://www.wikioi.com/problem/1307/ 题意:给你一个树,上面有n个节点,每个节点都有一个价值p,求一个n个节点的排列顺序,是的Σi*p[i]最小(要求父节点一定 ...

  6. [wikioi 1519]过路费(最小生成树+树链剖分)

    题目:http://www.wikioi.com/problem/1519/ 题意:给你一个连通的无向图,每条边都有权值,给你若干个询问(x,y),要输出从x到y的路径上边的最大值的最小值 分析:首先 ...

  7. 【wikioi】1012 最大公约数和最小公倍数问题

    题目链接 算法:辗转相除(欧几里得) gcd(a, b)是a和b最小公倍数, lcm(a, b)是a和b的最大公倍数 gcd(a, b) == gcd(b, a%b) 时间复杂度: O(lgb) 具体 ...

  8. 【wikioi】1250 Fibonacci数列(矩阵乘法)

    http://wikioi.com/problem/1250/ 我就不说这题有多水了. 0 1 1 1 矩阵快速幂 #include <cstdio> #include <cstri ...

  9. 【wikioi】1281 Xn数列(矩阵乘法)

    http://wikioi.com/problem/1281/ 矩阵真是个神奇的东西.. 只要搞出一个矩阵乘法,那么递推式可以完美的用上快速幂,然后使复杂度降到log 真是神奇. 在本题中,应该很快能 ...

随机推荐

  1. $stop and $finish in verilog

    $stop - Pauses the simulation, so you can resume it by using fg command in linux. In this case lince ...

  2. Orchard 学习-手动安装Orchard

    通过Orchard zip 文件手动配置网站 这篇文章将引导你如果通过Zip文件来安装Orchard. 我们会使用三种不同的方法来承载Orchard: IIS. WebMatrix and IIS E ...

  3. c#调用c++ dll(一)

    首先来说说c++中的dll 核心的一些知识 比较大的应用程序都由很多模块组成,这些模块分别完成相对独立的功能,它们彼此协作来完成整个软件系统的工作.可能存在一些模块的功能较为通用,在构造其它软件系统时 ...

  4. 当使用VS CODE 时,如果窗口中打开的文件无法识别HTML的话,可以使用以下方法添加要识别的文件类型

    找到该文件并修改\Microsoft VS Code\resources\app\extensions\html\package.json{ "name": "html& ...

  5. cognos 10.2.2 report studio数字---字符型查询注意事项

    做了一个简单的报表,就是按照员工编号查询员工,其中员工编号是全数字,我们保存在数据库中的是字符型varchar2(10),所以在report studio中做查询就一直报告服务器错误. 其中使用cas ...

  6. [firefly]暗黑源码解析

    一.架构 二.各模块详解 1.net 2.gate 3.game 4.db 三.启动 四.举例说明 五.性能测试

  7. 下载和安装cocoaPods

    ios中一些三方的库用的cocoaPods管理.管理三方库非常的方便 简单说一下安装步骤 1.sudo gem install cocoapods2.gem sources --remove http ...

  8. mysql error笔记1

    mysql视图问题: The user specified as a definer ('root'@'%') does not exist 原因:由于root用户对全局host无访问权限,给root ...

  9. 九度OJ 1124 Digital Roots -- 数位拆解

    题目地址:http://ac.jobdu.com/problem.php?pid=1124 题目描述: The digital root of a positive integer is found ...

  10. ASP.NET获取服务器文件的物理路径

    如下: string fullpath = context.Server.MapPath("hello.htm"); //得到hello.htm的全路径 string conten ...