zoukankan      html  css  js  c++  java
  • HDU-5113 Black And White

                            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.

    InputThe 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 . 
    OutputFor 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

     同14年北京站

    题意:t组数据,每组n,m,k. n行m列,k种颜色(1~k),然后输出一行1~k种颜色所能使用的次数。

    相同颜色不能相邻,上下左右。 如果可以输出YES和摆放的方法,否则NO

    思路:DFS+剪枝,从左上往右下搜索,按Z字型来搜

    剪枝:当剩下的颜色数大于所剩格子的一半,那么就直接return了。

    开始一直想的是搜四个方向,后面卡了很久,想到只要从左下搜到右下就行了。

    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    const int maxn = 1e6+10;
    const int MOD=1e9+7;
    int n,m,k;
    int c[30];
    int mapp[6][6];
    bool flag;
    bool check(int x,int y,int color)
    {
        if(c[color]&&mapp[x-1][y]!=color&&mapp[x][y-1]!=color)
            return true;
        return false;
    }
    void DFS(int x,int y,int step)
    {
        for(int i=1; i<=k; i++)
        {
            if(c[i]>((step+1)/2))
                return;
        }//没剪枝T
        if(step == 0)
        {
            flag = true;
        }
        if(flag)
            return;
        for(int i = 1; i <= k; i++)
        {
            if(check(x,y,i))
            {
                mapp[x][y] = i;
                c[i]--;
                if(y+1>m)
                    DFS(x + 1,1,step-1);
                else
                    DFS(x,y + 1,step-1);
                if(flag)
                    return;
                c[i]++;
                mapp[x][y] = -1;
            }
        }
    }
    int main()
    {
        int T;
        int p=0;
        scanf("%d",&T);
        while(T--)
        {
            flag = false;
            memset(mapp,-1,sizeof(mapp));
            scanf("%d%d%d",&n,&m,&k);
            for(int i = 1; i <= k; i++)
                scanf("%d",&c[i]);
            DFS(1,1,n*m);
            printf("Case #%d:
    ",++p);
            if(flag)
            {
                cout << "YES" << endl;
                for(int i = 1; i <= n; i++)
                {
                    for(int j = 1; j <= m; j++)
                        j==m?cout << mapp[i][j] << endl:cout<<mapp[i][j]<<" ";//wa了一发
                }
            }
            else
                cout << "NO" << endl;
        }
        return 0;
    }

    PS:摸鱼怪的博客分享,欢迎感谢各路大牛的指点~

  • 相关阅读:
    字符串和编码
    Python基础
    输入和输出
    Python代码运行助手
    使用文本编辑器
    第一个Python程序
    pycharm中创建并设置docker解释器
    docker 在windows上的使用
    Python time datetime模块
    Linux安装字体文件
  • 原文地址:https://www.cnblogs.com/MengX/p/9074643.html
Copyright © 2011-2022 走看看