题意:有N*M的棋盘,用K种颜色去染,要求相邻块不能同色。已知每种颜色要染的块数,问能不能染,如果能,输出任一种染法。

最开始dfs失败了- -,优先搜索一行,搜完后进入下一列,超时。本来以为搜索不行,看别人给的思路就是搜索+剪枝。
但是一直不知道该怎么剪,看了解题报告才发现,剩下的格子的数量+1必需是剩余最多种类棋子的两倍,否则必定会有相邻存在。
例如 3*3的空格中,一类棋子最多只能占5个、

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std; int num[20];
int tmap[10][10];
int n,m,k;
bool dfs(int x,int y)
{
for(int i = 1; i <= k; i++)
if((n*m-(m*(x-1)+y-1)+1)/2<num[i])
return false;
for(int i = 1; i <= k; i++)
{
bool flag = true;
if(y - 1>= 1)
{
if(tmap[x][y-1] == i)
flag = false;
}
if(x - 1>= 1)
{
if(tmap[x-1][y] == i)
flag = false;
}
if(num[i] > 0 && flag)
{
tmap[x][y] = i;
num[i]--;
if(x == n && m == y)
return true;
if(y + 1 <= m)
{
if(dfs(x,y+1))
return true;
}
else
{
if(x + 1 <= n)
if(dfs(x+1,1))
return true;
}
num[i]++;
tmap[x][y] = -1;
}
}
return false;
} int main()
{
int T;
scanf("%d",&T);
int cas = 1;
while(T--)
{
scanf("%d%d%d",&n,&m,&k);
for(int i = 1; i <= k; i++)
scanf("%d",&num[i]);
memset(tmap,-1,sizeof(tmap));
printf("Case #%d:\n",cas++);
if(dfs(1,1))
{
printf("YES\n");
for(int i=1; i<=n; i++)
{
for(int j=1; j<m; j++)
printf("%d ",tmap[i][j]);
printf("%d\n",tmap[i][m]);
}
}
else
printf("NO\n");
}
return 0;
}

  

hdu 5113(2014北京—搜索+剪枝)的更多相关文章

  1. HDU 5113--Black And White(搜索+剪枝)

    题目链接 Problem Description In mathematics, the four color theorem, or the four color map theorem, stat ...

  2. hdu 5119(2014北京)

    题意: 随机选择一个数,如果后面有比他小的就进行交换,直到没有为止(算一轮).求多少轮后为递增序列 思路: 倒着找,如果有比经过的最小数大的,ans+1 #include <iostream&g ...

  3. hdu 5112 (2014北京现场赛 A题)

    给出某个时刻对应的速度 求出相邻时刻的平均速度 输出最大值 Sample Input23 // n2 2 //t v1 13 430 31 52 0 Sample OutputCase #1: 2.0 ...

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

  5. hdu 5469 Antonidas(树的分治+字符串hashOR搜索+剪枝)

    题目链接:hdu 5469 Antonidas 题意: 给你一颗树,每个节点有一个字符,现在给你一个字符串S,问你是否能在树上找到两个节点u,v,使得u到v的最短路径构成的字符串恰好为S. 题解: 这 ...

  6. hdu 5887 搜索+剪枝

    Herbs Gathering Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  7. HDU 5113

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

  8. NOIP2015 斗地主(搜索+剪枝)

    4325: NOIP2015 斗地主 Time Limit: 30 Sec  Memory Limit: 1024 MBSubmit: 270  Solved: 192[Submit][Status] ...

  9. luogu 1731 搜索剪枝好题

    搜索剪枝这个东西真的是骗分利器,然鹅我这方面菜的不行,所以搜索数学dp三方面是真的应该好好训练一下 一本通的确该认真的刷嗯 #include<bits/stdc++.h> using na ...

随机推荐

  1. PID控制示例C编程

    typedef struct PID_zhs { double P,I,D;//P.I.D系数 float curError;//当前误差 float lastError;//上次误差 float p ...

  2. Tornado 网站demo 三

    模板 修改index.py #!/usr/bin/env Python # coding=utf-8 import tornado.web import methods.readdb as mrd c ...

  3. 利用Python爬取新浪微博营销案例库并下载到本地

    from bs4 import BeautifulSoup import requests,urllib.request,urllib.parse import json import time im ...

  4. 爬虫小探-Python3 urllib.request获取页面数据

    使用Python3 urllib.request中的Requests()和urlopen()方法获取页面源码,并用re正则进行正则匹配查找需要的数据. #forex.py#coding:utf-8 ' ...

  5. php的打印sql语句的方法

    echo M()->_sql(); 这样就可以调试当前生成的sql语句: //获取指定天的开始时间和结束时间 $datez="2016-05-12"; $t = strtot ...

  6. Windows中添加自己的程序到开机启动中(添加服务,添加注册表)

    在系统启动的时候启动自己想要启动的程序: 方法一:利用开机启动文件夹 将exe文件或exe文件的快捷方式复制到(启动)文件夹下 以win7为例:开始→所有程序→启动→鼠标右键打开 方法二:添加系统服务 ...

  7. Web系统Login拦截器

    所需要导入的包类:import org.springframework.web.servlet.HandleInterceptor;(拦截器要继承该类) public class loginInter ...

  8. leetcode算法:Trim a Binar Search Tree

    Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so that a ...

  9. 【WebGL入门】画一个旋转的cube

    最近搜罗了各种资料,发现WebGL中文网特别好用,很适合新手入门:http://www.hewebgl.com/article/getarticle/50 只需要下载好需要的所有包,然后用notepa ...

  10. Linux kernel 4.9及以上开启TCP BBR拥塞算法

    Linux kernel 4.9及以上开启TCP BBR拥塞算法 BBR 目的是要尽量跑满带宽, 并且尽量不要有排队的情况, 效果并不比速锐差 Linux kernel 4.9+ 已支持 tcp_bb ...