HDU 5113:Black And White(DFS)
题意
给出一个n*m的图,现在有k种颜色让你对这个图每个格子染色,每种颜色最多可以使用col[i]次,问是否存在一种染色方案使得相邻格子的颜色不同。
思路
以为是构造题,结果是爆搜。对于每一个点,如果可以往右边搜,那么就往右边走,如果右边走不了,就往下重新开一行搜。(否则最后可能不是所有格子都染上颜色)
只有一个剪枝:if((rem + 1) / 2 < col[i]) return ; (rem为剩下未染色的格子数),因为如果当前的颜色超过格子数的一半的时候,自己这个颜色必定和自己产生冲突。
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int INF = 0x3f3f3f3f;
const int N = 1e5 + 11;
int n, m, k, mp[30][30], col[30], flag;
bool check(int x, int y, int id) {
if(mp[x-1][y] != id && mp[x+1][y] != id && mp[x][y-1] != id && mp[x][y+1] != id) return true;
return false;
}
void dfs(int x, int y, int rem) {
if(!rem) { flag = 1; return ; }
for(int i = 1; i <= k; i++)
if((rem + 1) / 2 < col[i]) return ;
for(int i = 1; i <= k; i++) {
if(!col[i] || !check(x, y, i)) continue;
col[i]--; mp[x][y] = i;
if(y < m) dfs(x, y + 1, rem - 1);
else dfs(x + 1, 1, rem - 1);
if(flag) return ;
col[i]++; mp[x][y] = 0;
}
}
int main() {
int t; scanf("%d", &t);
for(int cas = 1; cas <= t; cas++) {
scanf("%d%d%d", &n, &m, &k);
for(int i = 1; i <= k; i++) scanf("%d", &col[i]);
memset(mp, 0, sizeof(mp));
flag = 0;
dfs(1, 1, n * m);
printf("Case #%d:\n", cas);
if(!flag) puts("NO");
else {
puts("YES");
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
printf("%d%c", mp[i][j], j == m ? '\n' : ' ');
}
// puts("");
} return 0;
}
HDU 5113:Black And White(DFS)的更多相关文章
- HDU 6060:RXD and dividing(DFS)
题目链接 题意 给出n个点,要把除1以外的点分成k个集合,然后对于每个集合要和1这个点一起求一个最小生成树,然后问这k个最小生成树的最大总和是多少. 思路 因为每个集合都包含1这个点,因此对于每个点都 ...
- POJ3009:Curling 2.0(dfs)
http://poj.org/problem?id=3009 Description On Planet MM-21, after their Olympic games this year, cur ...
- HDU 5795:A Simple Nim(博弈)
http://acm.hdu.edu.cn/showproblem.php?pid=5795 A Simple Nim Problem Description Two players take t ...
- 洛谷P1019:单词接龙(DFS)
题目描述 单词接龙是一个与我们经常玩的成语接龙相类似的游戏,现在我们已知一组单词,且给定一个开头的字母,要求出以这个字母开头的最长的"龙"(每个单词都最多在"龙" ...
- HDU 6188:Duizi and Shunzi(贪心)(广西邀请赛)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6188 题意 有n个数字,每个数字小于等于n,两个相同的数字价值为1,三个连续的数字价值为1 .问这n个 ...
- hdu 1010 Tempter of the Bone(dfs)
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- POJ1062:昂贵的聘礼(dfs)
昂贵的聘礼 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 58108 Accepted: 17536 题目链接:http ...
- HDU 3183:A Magic Lamp(RMQ)
http://acm.hdu.edu.cn/showproblem.php?pid=3183 题意:给出一个数,可以删除掉其中m个字符,要使得最后的数字最小,输出最后的数字(忽略前导零). 思路:设数 ...
- HDU 6011:Lotus and Characters(贪心)
http://acm.hdu.edu.cn/showproblem.php?pid=6011 题意:共有n种字符,每种字符有一个val和一个cnt,代表这个字符的价值和数量.可以制造的总价值是:第一个 ...
随机推荐
- Linking different libraries for Debug and Release builds in Cmake on windows?
问题叙述性说明: So I've got a library I'm compiling and I need to link different third party things in depe ...
- springboot 集成单元测试
官网参考地址 1. 添加依赖 <!-- 测试 --> <dependency> <groupId>org.springframework.boot</grou ...
- linux C Obstack
Obstack介绍 Obstack初始化 在Obstack中申请对象 释放对象 申请growing object 获取Obstack状态 数据对齐 以下是来自wiki对obstack的介绍: Obst ...
- Effection Go
Introduction: 新语言, 新思维 Formatting Indentation: 默认tab Line Length: 无限制, 会自动换行 Parentheses: 圆括号, 无限制, ...
- jquery多条件选择器
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- WPF控件的一些特殊应用
1 checkbox.IsChecked 返回的是bool?类型,需要用bool强转,或者直接和bool类型比较,将发生隐形转换 2 RadioButton有分组属性GroupName
- 读BeautifulSoup官方文档之html树的搜索(2)
除了find()和find_all(), 这里还提供了许多类似的方法我就细讲了, 参数和用法都差不多, 最后四个是next, previous是以.next/previous_element()来说的 ...
- jquery 包裹标签
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- iOS UIScrollView使用Autolayout
最近项目在迭代更新的时候,需要在之前用Autolayout写的界面里添加一个button,添加完这个button后,iPhone5,iPhone4显示不全了.遇到整个问题后很自然就想到了用UIScro ...
- 图像滤镜艺术----Brannan滤镜
原文:图像滤镜艺术----Brannan滤镜 作为第一篇文章,本人将介绍Instagram中Brannan 滤镜的实现过程,当然,是自己的模拟而已,结果差异敬请谅解. 先看下效果图: ...