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. Linux操作系统搭建LAMP开发环境

    Step1. 安装 Apache 在terminal中输入命令 sudo apt-get install apache2 打开浏览器,在地址栏输入:127.0.0.1,如果出现了 “It works! ...

  2. 盘点 Github 所用到的开源项目

    http://www.php100.com/html/it/mobile/2014/0401/6736.html 在致力于开源事业的同时,Github也使用一些非常优秀的开源项目的来打造自己的平台与服 ...

  3. 16Aspx.com源码2014年7月详细

            Web电子商务网(三层)V2.0源码 2014-07-31   [VS2010] 源码介绍: Web电子商务网(三层)V2.0源码 源码描述: 一.源码特点     采用三层架构开发, ...

  4. ACM——A + B Problem (4)

    A + B Problem (4) 时间限制(普通/Java):1000MS/3000MS          运行内存限制:65536KByte总提交:2496            测试通过:124 ...

  5. 趣拍proguard配置

    # Add project specific ProGuard rules here.# By default, the flags in this file are appended to flag ...

  6. C# 的static与单例模式

    C# 的static与单例模式 static是静态对象,在类被第一次使用,或者第一次被实例化时执行 /// <summary> /// 线程安全的单件模式 /// </summary ...

  7. Ajax实现页面后台button click事件无刷新弹窗

    很多人在做链接弹出新窗口的时候,都习惯用Response.Wrtite("<script>window.open('a.aspx')</script>") ...

  8. excel导入 导出 兼容各个版本服务器不装EXCEL也可以

    给出 demo源码: http://pan.baidu.com/s/1hqGMudY 提取码:pw4n首先要引用 NPOI.dll (可在网上下载!) //导入 public void OnSubmi ...

  9. 类库探源——System.Math 和 Random

    一.System.Math Math类:为三角函数.对数函数和其他通用数学函数提供常数和静态方法 命名空间: System 程序集 :   mscorlib.dll 继承关系: 常用属性: Math. ...

  10. javascript DOM操作 第19节

    <html> <head> <title>DOM对象</title> <script type="text/javascript&quo ...