http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1508

题意:地图中四联通的块是一个国家,A和B每个人可以涂两种颜色,且B不能涂超过5次,相邻的国家不能涂一样的颜色,并且最后每种颜色都要用上,问共有多少种涂色方案。

思路:先DFS处理出图中的国家,然后先用一个邻接矩阵存下相邻的国家(直接用邻接表会有重边),然后再用邻接表存图。数方案个数的时候,假设共有a,b,c,d这四种颜色,A可以涂a,b,B可以涂c,d。因为颜色之间没有差异,所以A先涂a颜色,B先涂c颜色,那么和A先涂b颜色,B先涂c或者d颜色是一样的。所以暂时假设A先涂a颜色,B先涂c颜色,然后利用平时判断二分图一样,去染色,记得染完色之后col[num]要置0。通过约束条件得到答案后,因为有先涂a-c,a-d,b-c,b-d这四种情况,所以最后答案*4。

 #include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <iostream>
#include <stack>
#include <map>
#include <queue>
#include <set>
using namespace std;
typedef long long LL;
#define N 40
#define INF 0x3f3f3f3f
struct node {
int v, nxt;
}edge[N*N*];
int n, m, vis[N][N], tu[N][N], cnt, col[N], head[N], ans, tot, dx[] = {, -, , }, dy[] = {, , , -};
char mp[N][N]; bool check(int x, int y) { if( < x && x <= n && < y && y <= m) return true; return false; } void Add(int u, int v) { edge[tot].v = v; edge[tot].nxt = head[u]; head[u] = tot++; } void DFS(int x, int y, char c) {
for(int i = ; i < ; i++) {
int nx = dx[i] + x, ny = dy[i] + y;
if(check(nx, ny) && !vis[nx][ny] && mp[nx][ny] == c) {
vis[nx][ny] = cnt; DFS(nx, ny, c);
}
}
} bool judge(int u, int c) {
for(int i = head[u]; ~i; i = edge[i].nxt)
if(col[edge[i].v] == c) return false;
return true;
} void dfs(int num, int a, int b, int c, int d) {
if(num == cnt + ) {
if(a && b && c && d) ans++;
return ;
}
if(judge(num, )) {
col[num] = ;
dfs(num + , a + , b, c, d);
col[num] = ;
}
if(a > && judge(num, )) {
col[num] = ;
dfs(num + , a, b + , c, d);
col[num] = ;
}
if(c + d < && judge(num, )) {
col[num] = ;
dfs(num + , a, b, c + , d);
col[num] = ;
}
if(c + d < && c && judge(num, )) {
col[num] = ;
dfs(num + , a, b, c, d + );
col[num] = ;
}
} int main()
{
int cas = ;
while(~scanf("%d%d", &n, &m)) {
for(int i = ; i <= n; i++) scanf("%s", mp[i] + );
memset(head, -, sizeof(head));
memset(vis, , sizeof(vis));
memset(col, , sizeof(col));
memset(tu, , sizeof(tu));
tot = cnt = ans = ;
for(int i = ; i <= n; i++)
for(int j = ; j <= m; j++)
if(!vis[i][j]) { cnt++; vis[i][j] = cnt; DFS(i, j, mp[i][j]); }
for(int i = ; i <= n; i++) {
for(int j = ; j <= m; j++) {
if(vis[i-][j] && vis[i-][j] != vis[i][j]) tu[vis[i][j]][vis[i-][j]] = tu[vis[i-][j]][vis[i][j]] = ;
if(vis[i][j-] && vis[i][j-] != vis[i][j]) tu[vis[i][j-]][vis[i][j]] = tu[vis[i][j]][vis[i][j-]] = ;
}
}
for(int i = ; i <= cnt; i++)
for(int j = ; j <= cnt; j++)
if(i != j && tu[i][j]) Add(i, j);
dfs(, , , , );
printf("Case %d: %d\n", cas++, ans * );
}
return ;
}

CSU 1508:地图的四着色(DFS+剪枝)的更多相关文章

  1. [csu1508 地图的四着色]二分图染色

    抽象后的题意:给一个不超过30个点的图,A从中选不超过5个点涂红绿两种颜色,B用黑白两种颜色把剩下的涂完,任意一条边两端的颜色不同,求每种颜色至少用涂一次的方案数 思路:枚举A涂的点的集合,将原图分成 ...

  2. DFS+剪枝 HDOJ 5323 Solve this interesting problem

    题目传送门 /* 题意:告诉一个区间[L,R],问根节点的n是多少 DFS+剪枝:父亲节点有四种情况:[l, r + len],[l, r + len - 1],[l - len, r],[l - l ...

  3. 数据结构与算法实验题 9.1 K 歌 DFS+剪枝

    数据结构与算法实验题 K 歌 ★实验任务 3* n 个人(标号1~ 3 * n )分成 n 组 K 歌.有 m 个 3 人组合,每个组合都对应一个分数,你能算出最大能够得到的总分数么? ★数据输入 输 ...

  4. *HDU1455 DFS剪枝

    Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  5. POJ 3009 DFS+剪枝

    POJ3009 DFS+剪枝 原题: Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16280 Acce ...

  6. poj 1724:ROADS(DFS + 剪枝)

    ROADS Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10777   Accepted: 3961 Descriptio ...

  7. DFS(剪枝) POJ 1011 Sticks

    题目传送门 /* 题意:若干小木棍,是由多条相同长度的长木棍分割而成,问最小的原来长木棍的长度: DFS剪枝:剪枝搜索的好题!TLE好几次,终于剪枝完全! 剪枝主要在4和5:4 相同长度的木棍不再搜索 ...

  8. HDU 5952 Counting Cliques 【DFS+剪枝】 (2016ACM/ICPC亚洲区沈阳站)

    Counting Cliques Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  9. HDU 5937 Equation 【DFS+剪枝】 (2016年中国大学生程序设计竞赛(杭州))

    Equation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

随机推荐

  1. 合并 && 还原属性链

    效果 原数据 { "id": 10, "text": { "title": "title", "content ...

  2. 【C/S通信交互之Http篇】Cocos2dx(Client)使用Curl与Jetty(Server)实现手机网游Http通信框架(内含解决curl.h头文件找不到问题)

    之前已经分享过一篇基于Cocos2dx与服务器使用Socket进行通信的框架,还不太熟悉的请移步到如下博文中: [C/S通信交互之Socket篇]Cocos2dx(Client)使用BSD Socke ...

  3. Java Policy

    # What The policy for a Java™ programming language application environment (specifying which permiss ...

  4. .Net 开源服务 and Net站点

    小泥鳅博客系统也是一个.NET平台的开源免费博客系统,创建于2008年夏天,基于.Net平台开发,拥有完整的文章发布,评论,订阅,标签等功能,满足个人/团队信息发布需求,可作为Blog,CMS,甚至建 ...

  5. WPF常见内存泄露

    Event handlers leak This type of leak occurs when subscribing an object (let's call it listener) to ...

  6. 图像滤镜艺术---(Punch Filter)交叉冲印滤镜

    原文:图像滤镜艺术---(Punch Filter)交叉冲印滤镜 (Punch Filter)交叉冲印滤镜 本文介绍一种交叉冲印效果的代码实现,至于原理,不在累赘,直接看代码:  int f_TPun ...

  7. Image Captioning 经典论文合辑

    Image Caption: Automatically describing the content of an image domain:CV+NLP Category:(by myself, y ...

  8. HTTP协议解析(格式和举例十分清楚)

    掌握HTTP虽然不是必须的,但是如果你知道它的工作原理,那么在学习JSP开发中的某些知识就可以易如反掌了. 一,HTTP协议详解之URL篇 http(超文本传输协议)是一个基于请求与响应模式的.无状态 ...

  9. DataGridView 中发生以下异常: System.Exception: 是 不是 Decimal 的有效值。 ---> System.FormatException: 输入字符串的格式不正确。

    其实之前我自己是没测出这个问题的,但是一放到测试的手上就出来了,原因我知道在哪里改输什么东西,但是人家不知道啊.报错如下: --------------------------- “DataGridV ...

  10. C# 实现系统关机、注销、重启、休眠、挂起

    原文:C# 实现系统关机.注销.重启.休眠.挂起 核心代码如下: using System; using System.Text; using System.Diagnostics; using Sy ...