Black And White (染色问题,剪枝)

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 c i cells. 

Matt hopes you can tell him a possible coloring.

Input

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 c i (c i > 0), denoting the number of cells where the i-th color should be used. 

It’s guaranteed that c 1 + c 2 + · · · + c K = N × M . 

Output

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.

Sample Input

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

Sample Output

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

题意:T组数据,每组数据一个矩阵大小n,m,还有一个颜色种类数K,紧跟着有K种颜色的数量,问能否染色成功,输出一种即可。

思路:一个dfs不过要进行剪枝,就是每次要判断是否所有颜色都有可以完整染色的可能;

eg:不完整的染色,已知A还剩5个,那么最少我们还需要3个其他颜色才能让A不相邻。即  col[i]>(n*m-step+1)/2

代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n,m,k,flag,col[100];
int Map[10][10];
int dir[4][2]= {0,1,1,0,0,-1,-1,0};
bool check(int c,int x,int y)
{
    for(int i=0; i<4; i++)
    {
        int tx=x+dir[i][0];
        int ty=y+dir[i][1];
        if(c==Map[tx][ty])
            return false;
    }
    return true;
}
void dfs(int x,int y,int step)
{
    if(flag)return;
    if(step==n*m)
    {
        flag=1;
        printf("YES\n");
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<m; j++)
                printf("%d ",Map[i][j]);
            printf("%d\n",Map[i][m]);
        }
        return;
    }
    for(int i=1; i<=k; i++) //如果到达这个状态的时候,有些颜色剩余过多就不能完成完整的染色
    {
        //如果还剩颜色a(5个),那么最少还需其他颜色(3个)才能保证a不会相邻
        if(col[i]>(n*m-step+1)/2)
            return;
    }
    for(int i=1; i<=k; i++)
    {
        if(col[i]>0&&check(i,x,y))
        {
            int tx=x;
            int ty=y+1;
            if(ty>m){tx++,ty=1;}
            Map[x][y]=i;
            col[i]--;
            dfs(tx,ty,step+1);
            Map[x][y]=-1;
            col[i]++;
        }
    }
}
int main()
{
    int t,cas=1;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d%d",&n,&m,&k);
        memset(Map,-1,sizeof(Map));
        for(int i=1; i<=k; i++)
            scanf("%d",&col[i]);
        flag=0;
        printf("Case #%d:\n",cas++);
        dfs(1,1,0);
        if(!flag)printf("NO\n");
    }
    return 0;
}

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
回溯法是解决01背包问题的一种常用方法,但是在处理大规模问题时,回溯法的效率会变得很低。因此,我们需要使用剪枝技术来优化回溯法的效率。 剪枝是指在搜索过程中,根据问题的特点,提前判断某些分支不可能得到最优解,从而减少搜索的分支数,提高搜索效率。在01背包问题中,我们可以使用以下两种剪枝技术: 1.重量剪枝:如果当前物品加入背包后,背包的重量已经超过了背包的容量,那么就可以直接剪枝,不再继续搜索。 2.限界剪枝:在搜索过程中,我们可以计算出当前背包中已经装入物品的总价值,以及剩余物品的最大价值和。如果当前背包中已经装入物品的总价值加上剩余物品的最大价值和小于当前最优解的价值,那么就可以直接剪枝,不再继续搜索。 下面是使用剪枝技术的回溯法解决01背包问题的Python代码: ```python def backtrack(i, cw, cv): global max_value if i == n or cw == w: if cv > max_value: max_value = cv return if cw + items[i][0] <= w: backtrack(i+1, cw+items[i][0], cv+items[i][1]) if cv + bound(i+1, cw) > max_value: backtrack(i+1, cw, cv) def bound(i, cw): if cw >= w: return 0 max_value = 0 total_weight = 0 for j in range(i, n): if total_weight + items[j][0] <= w: total_weight += items[j][0] max_value += items[j][1] else: remain_weight = w - total_weight max_value += items[j][1] * (remain_weight / items[j][0]) break return max_value if __name__ == '__main__': items = [(2, 3), (3, 4), (4, 5), (5, 6)] n = len(items) w = 8 max_value = 0 backtrack(0, 0, 0) print(max_value) ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值