http://acm.hdu.edu.cn/showproblem.php?pid=5113

题意:给你n*m的格子,然后在每个格子内涂色,相邻格子不能同色,然后给你每个颜色涂的格子的固定个数,然后可不可以实现,可以实现输出任意一种,否则输出NO

思路:dfs枚举,剪纸,每种颜色剩余的个数不能超过剩余格子数的一半,如果剩余格子数是奇数,不能超过一半加1,偶数是一半。

 #include <cstdio>
#include <cstring>
#include <algorithm>
#define maxn 100
using namespace std; struct node
{
int c;
int id;
bool operator<(const node &a)const
{
return c>a.c;
}
}p[];
int t,n,m,k;
int c[maxn];
int g[][];
bool flag1; void dfs(int cc)
{
if(flag1) return;
if(cc>=n*m) return ;
for(int i=; i<=k; i++)
{
if((n*m-cc)%==)
{
if(p[i].c>(n*m-cc)/) return;
}
else
{
if(p[i].c>(n*m-cc)/+) return;
}
}
int x=cc/m;
int y=cc%m;
for(int i=; i<=k; i++)
{
if((g[x-][y]==p[i].id&&x->=)||(g[x][y-]==p[i].id&&y->=)) continue;
if(p[i].c==) continue;
g[x][y]=p[i].id;
p[i].c--;
dfs(cc+);
p[i].c++;
if(cc==n*m-)
{
flag1=true;
printf("YES\n");
for(int xx=; xx<n; xx++)
{
for(int yy=; yy<m; yy++)
{
if(yy==) printf("%d",g[xx][yy]);
else printf(" %d",g[xx][yy]);
}
printf("\n");
}
}
}
} int main()
{
scanf("%d",&t);
for(int cas=; cas<=t; cas++)
{
scanf("%d%d%d",&n,&m,&k);
for(int i=; i<=k; i++)
{
scanf("%d",&c[i]);
p[i].c=c[i];
p[i].id=i;
}
sort(p+,p+k+);
bool flag=true;
for(int i=; i<k; i++)
{
if((n*m)%==)
{
if(c[i]>(n*m/))
{
flag=false;
break;
}
}
else
{
if(c[i]>((n*m)/+))
{
flag=false;
break;
}
}
}
printf("Case #%d:\n",cas);
if(!flag)
{
printf("NO\n");
continue;
}
flag1=false;
dfs();
if(!flag1) printf("NO\n");
}
return ;
}

hdu 5113 Black And White的更多相关文章

  1. HDU 5113 Black And White 回溯+剪枝

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5113 Black And White Time Limit: 2000/2000 MS (Java/ ...

  2. [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) ...

  3. 搜索(剪枝优化):HDU 5113 Black And White

    Description In mathematics, the four color theorem, or the four color map theorem, states that, give ...

  4. HDU 5113 Black And White ( 2014 北京区预赛 B 、搜索 + 剪枝 )

    题目链接 题意 : 给出 n * m 的网格.要你用 k 种不同的颜色填给出的网格.使得相邻的格子颜色不同.若有解要输出具体的方案 分析 : 看似构造.实则搜索.手构构半天没有什么好想法 直接搜就行了 ...

  5. hdu 3911 Black And White(线段树)

    题目连接:hdu 3911 Black And White 题目大意:给定一个序列,然后有M次操作: 0 l r:表示询问l,r中最大连续1的个数 1 l r:表示将l,r区间上的数取反 解题思路:线 ...

  6. HDU 5113

    HDU 5113类似四色定理的什么东西,大体就是dfs了,不过有两个坑点,这个题的逼格瞬间就上去了1.剪枝很神奇,任何一种颜色都不能超过剩下总格子数的一半,想想确实显然但是比赛的时候没有想到:2.测评 ...

  7. Hdu OJ 5113 Black And White (2014ACM/ICPC亚洲区北京站) (搜索)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5113 题目大意:有k种颜色的方块,每种颜色有ai个, 现在有n*m的矩阵, 问这k种颜色的方块能否使任 ...

  8. HDU 5113:Black And White(DFS)

    题目链接 题意 给出一个n*m的图,现在有k种颜色让你对这个图每个格子染色,每种颜色最多可以使用col[i]次,问是否存在一种染色方案使得相邻格子的颜色不同. 思路 以为是构造题,结果是爆搜.对于每一 ...

  9. HDU 3911 Black And White 分段树 题解

    Problem Description There are a bunch of stones on the beach; Stone color is white or black. Little ...

随机推荐

  1. 23web app实现上下左右滑动

    转载请说明出处:http://blog.csdn.net/wowkk/article/category/1619287 (创意系列) /*最近项目须要苹果电脑,假设您支持学生创业并愿意赞助我们一台,请 ...

  2. Java堆栈详解 .

    1. Java中堆栈(stack)和堆(heap) (1)内存分配的策略 按照编译原理的观点,程序运行时的内存分配有三种策略,分别是静态的,栈式的,和堆式的. 静态存储分配是指在编译时就能确定每个数据 ...

  3. android 35 ListView增删改差

    MainActivity package com.sxt.day05_11; import java.util.ArrayList; import java.util.List; import and ...

  4. java按照集合中元素的属性进行排序示例代码

    public class Student { private String name; private int age; private int id; public Student() {  sup ...

  5. DIV------使用 <div> 元素的网页布局

    <!DOCTYPE html> <html> <head> <style type="text/css"> div#containe ...

  6. codevs 1519 过路费 最小生成树+倍增

    /*codevs 1519 过路费 最小生成树+倍增*/ #include<iostream> #include<cstdio> #include<cstring> ...

  7. 分离数据库(Detach database).

    Many times, we often needs to detach our databases if we want to copy it to another database instanc ...

  8. Oracle实用技巧

    一. ORACLE SQL PLUS 使用技巧: ----①查找重复记录: SELECT DRAWING, DSNOFROM EM5_PIPE_PREFABWHERE ROWID!= (SELECT ...

  9. oracle解析xml(增加对9i版本的支持)

    --方法1  SELECT * FROM  XMLTABLE('$B/DEAL_BASIC/USER_DEAL_INFO' PASSING     XMLTYPE('<?xml version= ...

  10. 一个Socket数据处理模型

    Socket编程中,如何高效地接收和处理数据,这里介绍一个简单的编程模型. Socket索引 - SocketId 在给出编程模型之前,先提这样一个问题,程序中如何描述Socket连接? 为什么这么问 ...