HDU 5113 Black And White 回溯+剪枝
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=5113
Black And White
Time Limit: 2000/2000 MS (Java/Others)Memory Limit: 512000/512000 K (Java/Others)
#### 问题描述
> In mathematics, the four color theorem, or the four color map theorem, states that, given any separation of a plane into contiguous regions, producing a figure called a map, no more than four colors are required to color the regions of the map so that no two adjacent regions have the same color.
> — Wikipedia, the free encyclopedia
>
> In this problem, you have to solve the 4-color problem. Hey, I’m just joking.
>
> You are asked to solve a similar problem:
>
> Color an N × M chessboard with K colors numbered from 1 to K such that no two adjacent cells have the same color (two cells are adjacent if they share an edge). The i-th color should be used in exactly ci cells.
>
> Matt hopes you can tell him a possible coloring.
输入
The first line contains only one integer T (1 ≤ T ≤ 5000), which indicates the number of test cases.
For each test case, the first line contains three integers: N, M, K (0 < N, M ≤ 5, 0 < K ≤ N × M ).
The second line contains K integers ci (ci 0), denoting the number of cells where the i-th color should be used.
It’s guaranteed that c1 + c2 + · · · + cK = N × M .
输出
For each test case, the first line contains “Case #x:”, where x is the case number (starting from 1).
In the second line, output “NO” if there is no coloring satisfying the requirements. Otherwise, output “YES” in one line. Each of the following N lines contains M numbers seperated by single whitespace, denoting the color of the cells.
If there are multiple solutions, output any of them.
样例输入
4
1 5 2
4 1
3 3 4
1 2 2 4
2 3 3
2 2 2
3 2 3
2 2 2
样例输出
Case #1:
NO
Case #2:
YES
4 3 4
2 1 2
4 3 4
Case #3:
YES
1 2 3
2 3 1
Case #4:
YES
1 2
2 3
3 1
题意
给你n*m的方格,你要用k种颜色去填,每种颜色要填ci次,并且要保证没有任意两个相邻的格子有相同的颜色,构造一种可行方法,如果不存在输出-1.
题解
直接暴力回溯+剪枝,当没填的颜色中同种颜色数量超过(剩余格子+1)/2的时候,不可能合法。
代码
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define mid (l+(r-l)/2)
#define sz() size()
#define pb(v) push_back(v)
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=a;i<(b);i++)
#define scf scanf
#define prf printf
typedef int LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<pair<int,int> > VPII;
const int INF=0x3f3f3f3f;
const LL INFL=10000000000000000LL;
const double eps=1e-9;
const double PI = acos(-1.0);
//start----------------------------------------------------------------------
const int maxn=33;
int n,m,k;
int arr[maxn];
int mat[7][7];
bool su;
const int dx[]={-1,1,0,0};
const int dy[]={0,0,-1,1};
bool ok(int x,int y,int color){
for(int i=0;i<4;i++){
int nx=x+dx[i];
int ny=y+dy[i];
if(nx<0||nx>=n||ny<0||ny>=m) continue;
if(mat[nx][ny]==color) return false;
}
return true;
}
void dfs(int sta){
if(sta==n*m){
su=true;
prf("YES\n");
rep(i,0,n){
rep(j,0,m){
prf("%d",mat[i][j]);
if(j==m-1) prf("\n");
else prf(" ");
}
}
return;
}
///剪枝
int ma=0;
for(int i=1;i<=k;i++) ma=max(ma,arr[i]);
if(ma>(n*m-sta+1)/2) return;
///回溯
int x=sta/m,y=sta%m;
for(int i=1;i<=k;i++){
if(arr[i]==0) continue;
if(ok(x,y,i)){
mat[x][y]=i;
arr[i]--;
dfs(sta+1);
if(su) return;
arr[i]++;
mat[x][y]=-1;
}
}
}
void init(){
clr(mat,-1);
}
int main() {
int tc,kase=0;
scf("%d",&tc);
while(tc--) {
init();
scf("%d%d%d",&n,&m,&k);
for(int i=1;i<=k;i++){
scf("%d",&arr[i]);
}
prf("Case #%d:\n",++kase);
su=false;
dfs(0);
if(!su) prf("NO\n");
}
return 0;
}
//end-----------------------------------------------------------------------
HDU 5113 Black And White 回溯+剪枝的更多相关文章
- [HDU 5113] Black And White (dfs+剪枝)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5113 题目大意:给你N*M的棋盘,K种颜色,每种颜色有c[i]个(sigma(c[i]) = N*M) ...
- HDU 2553 N皇后问题(回溯 + 剪枝)
本文链接:http://i.cnblogs.com/EditPosts.aspx?postid=5398797 题意: 在N*N(N <= 10)的方格棋盘放置了N个皇后,使得它们不相互攻击(即 ...
- 搜索(剪枝优化):HDU 5113 Black And White
Description In mathematics, the four color theorem, or the four color map theorem, states that, give ...
- HDU 5113 Black And White ( 2014 北京区预赛 B 、搜索 + 剪枝 )
题目链接 题意 : 给出 n * m 的网格.要你用 k 种不同的颜色填给出的网格.使得相邻的格子颜色不同.若有解要输出具体的方案 分析 : 看似构造.实则搜索.手构构半天没有什么好想法 直接搜就行了 ...
- hdu 5113 Black And White
http://acm.hdu.edu.cn/showproblem.php?pid=5113 题意:给你n*m的格子,然后在每个格子内涂色,相邻格子不能同色,然后给你每个颜色涂的格子的固定个数,然后可 ...
- hdoj 5113 Black And White DFS+剪枝
Black And White Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 512000/512000 K (Java/Others) T ...
- hdu 5113(2014北京—搜索+剪枝)
题意:有N*M的棋盘,用K种颜色去染,要求相邻块不能同色.已知每种颜色要染的块数,问能不能染,如果能,输出任一种染法. 最开始dfs失败了- -,优先搜索一行,搜完后进入下一列,超时.本来以为搜索不行 ...
- HDU 5113
HDU 5113类似四色定理的什么东西,大体就是dfs了,不过有两个坑点,这个题的逼格瞬间就上去了1.剪枝很神奇,任何一种颜色都不能超过剩下总格子数的一半,想想确实显然但是比赛的时候没有想到:2.测评 ...
- hdu 3911 Black And White(线段树)
题目连接:hdu 3911 Black And White 题目大意:给定一个序列,然后有M次操作: 0 l r:表示询问l,r中最大连续1的个数 1 l r:表示将l,r区间上的数取反 解题思路:线 ...
随机推荐
- UESTC 288 青蛙的约会 扩展GCD
设两只青蛙跳了t步,则此时A的坐标:x+mt,B的坐标:y+nt.要使的他们在同一点,则要满足: x+mt - (y+nt) = kL (p是整数) 化成: (n-m)t + kL = x-y (L ...
- UMLUnified Modeling Language (UML)又称统一建模语言或标准建模语言
1.用例图(use case diagram) 2.活动图(activity diagram) 3.静态结构图 4.顺序图(Sequence Diagram):时序图 5.交互纵览图(Interact ...
- java 21 - 12 IO流的打印流
打印流 字节流打印流 PrintStream 字符打印流 PrintWriter打印流的特点: A:只有写数据的,没有读取数据.只能操作目的地,不能操作数据源.(只能写入数据到文件中,而不能从文件中提 ...
- java10-1 Object类
Object:类 Object 是类层次结构的根类.每个类都使用 Object 作为超类. 每个类都直接或者间接的继承自Object类. Object类的方法: public int has ...
- swift代理使用
代理声明: //oc调用代理 @objc(NurseListCellDelegate) protocol NurseListCellDelegate : NSObjectProtocol{ func ...
- sqlserver数据库附加报错5120
sqlserver数据库的数据文件分离.附加是很方便的功能. 有时候在附加文件时报错,是因为数据库文件权限不够,给Authenticated Users用户增加完全控制权限即可.
- work_queue 函数调用栈
init_workqueues ---> create_worker --> kthread_create_on_node
- 没有什么好神秘的: wait_on_page_bit
文件系统中经常会有wait_on_page_bit函数的封装,比如f2fs中就会有如下的代码: 1431 void f2fs_wait_on_page_writeback(struct page *p ...
- STL整理
sort 升序排列: iterator lower_bound( const key_type &key ): 返回一个迭代器,指向键值>= key的第一个元素. iterator up ...
- HttpServletRequest 中 getRequestURL和getRequestURI的区别
比如说有这样的一个页面 test1.jsp======================= <a href ="test.jsp?name=wf">跳转到test2.js ...