HDU 5113--Black And White(搜索+剪枝)
— 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.
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 .
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.
题意:有一个N*M的方格板,现在要在上面的每个方格上涂颜色,有K种颜色,每种颜色分别涂c[1]次、c[2]次……c[K]次,c[1]+c[2]+……+c[K]=N*M
要求每个方格的颜色与其上下左右均不同,如果可以输出YES,并且输出其中的一种涂法,如果不行,输出NO;
思路:暴力搜索,但是这样会超时,可以在搜索中加入剪枝:对于剩余的方格数res,以及当前剩余的颜色可涂数必须满足(res+1)/2>=c[i]
否则在当前情况下继续向下搜得不到正确涂法;
代码如下:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
int N,K,M;
int c[];
int mp[][]; int check(int x,int y,int k)
{
int f=;
if(mp[x-][y]==k) f=;
if(mp[x][y-]==k) f=;
return f;
} int cal(int x,int y)
{
if(x>N) return ;
int res=(N-x)*M+M-y+; ///剩余方格数+1 ;
for(int i=;i<=K;i++) if(res/<c[i]) return ; ///剪枝,某种颜色剩余方格数>(剩余方格数+1)/2 肯定不对;
for(int i=;i<=K;i++)
{
int f=;
if(c[i]&&check(x,y,i)){
mp[x][y]=i; c[i]--;
if(y==M) f=cal(x+,);
else f=cal(x,y+);
c[i]++;
}
if(f) return ;
}
return ;
} int main()
{
int T,Case=;
cin>>T;
while(T--)
{
scanf("%d%d%d",&N,&M,&K);
for(int i=;i<=K;i++) scanf("%d",&c[i]);
printf("Case #%d:\n",Case++); if(!cal(,)) { puts("NO"); continue; }
puts("YES");
for(int i=;i<=N;i++)
for(int j=;j<=M;j++)
printf("%d%c",mp[i][j],(j==M)?'\n':' ');
}
return ;
}
HDU 5113--Black And White(搜索+剪枝)的更多相关文章
- HDU 5113 Black And White 回溯+剪枝
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5113 Black And White Time Limit: 2000/2000 MS (Java/ ...
- [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 5113(2014北京—搜索+剪枝)
题意:有N*M的棋盘,用K种颜色去染,要求相邻块不能同色.已知每种颜色要染的块数,问能不能染,如果能,输出任一种染法. 最开始dfs失败了- -,优先搜索一行,搜完后进入下一列,超时.本来以为搜索不行 ...
- 搜索(剪枝优化):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 6171】Admiral(搜索+剪枝)
多校10 1001 HDU 6171 Admiral 题意 目标状态是第i行有i+1个i数字(i=0-5)共6行.给你初始状态,数字0可以交换上一行最近的两个和下一行最近的两个.求20步以内到目标状态 ...
- HDU 1045 Fire Net(搜索剪枝)
http://acm.split.hdu.edu.cn/showproblem.php?pid=1045 http://acm.hdu.edu.cn/showproblem.php?pid=1045 ...
- 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 ...
随机推荐
- Python学习—爬虫篇之破解ntml登陆问题
之前帮公司爬取过内部的一个问题单网站,要求将每个问题单的下的附件下载下来.一开始的时候我就遇到一个破解登陆验证的大坑...... (╬ ̄皿 ̄)=○ 由于在公司使用的都是内网,代码和网站的描述 ...
- time模块的使用
https://www.cnblogs.com/jimmy-share/p/10605575.html import time 一.方法汇总: time.sleep():定时函数 time.time( ...
- java_24 FileOutputStream类和FileInputStream类
1.OutputStream 和InputStream 输入和输出:1.参照物都是java程序来惨遭 2.Input输入,持久化上的数据---->内存 3.Output输出,内存--->硬 ...
- python基础 (编码进阶,文件操作和深浅copy)
1.编码的进阶 字符串在Python内部的表示是unicode编码,因此,在做编码转换时,通常需要以unicode作为中间编码. 即先将其他编码的字符串解码(decode)成unicode,再从uni ...
- dbus 消息和消息总线实例讲解-一
应用程序A和消息总线连接,这个连接获取了一个众所周知的公共名(记作连接A).应用程序A中有对象A1提供了接口I1,接口I1有方法M1. 应用程序B和消息总线连接,要求调用连接A上对象A1的接口I1的方 ...
- windbg 经典死锁调试
代码 // Deadlock_Debug.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include "windows.h& ...
- 第二阶段第一次spring会议
昨天我们讨论了改进方法,打算建立数据库. 今天我对39个组发表了建议以及总结了改进意见和改进方案. 明天我将对软件添加回收站.
- python之全局变量和局部变量
一.定义 1.全局变量 定义在函数外部一级代码的变量,叫全局变量,全局能用. 2.局部变量 定义在函数内的变量,只能在局部生效 二.用法 1.在函数内部可以引用全局变量,如果全局和局部都有一个变量na ...
- Mac could not read from remote repository
IDE clone数据的时候要使用SSH,不使用HTTPS,就解决了问题
- 理解React组件的生命周期
本文作者写作的时间较早,所以里面会出现很多的旧版ES5的时代的方法.不过,虽然如此并不影响读者理解组件的生命周期.反而是作者分为几种不同的触发机制来解释生命周期的各个方法,让读者更加容易理解涉及到的概 ...