zoukankan      html  css  js  c++  java
  • Minieye杯第十五届华中科技大学程序设计邀请赛网络赛D Grid(简单构造)

    链接:https://ac.nowcoder.com/acm/contest/560/D
    来源:牛客网

    题目描述

    Give you a rectangular gird which is h cells high and w cells wide.
    Each grid is black initially. You can turn some grids into white.
    A grid A(x,y) is connected with grid B if the coordinate of B is (x+1, y),(x-1, y),(x, y+1) or (x, y-1).
    And your task is to propose a plan of the gird which has exactly n connected components of black part.
    If there is no valid plan containing n connected components of black part, output -1.

    输入描述:

    Three integers h, w, n(1h,w200,1n109)(1≤h,w≤200,1≤n≤109) as described above.

    输出描述:

    Print h rows and w columns, '#' represents a black grid and '*' represents a white grid, indicating your solution.
    示例1

    输入

    复制
    1 10 5

    输出

    复制
    #*#*#*#*#*

    题意:
    给你一个高h,宽w,一个数量k。让你构建一个h*w的数组,使之只含有两种元素,黑和白,即#和*
    要求黑色块的联通集个数刚好是K。

    思路:显然每一行中交叉填黑白,换行后每一行交叉填白黑,这样可以让这个h*w的数组中出现最大数量的黑色的联通块。

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #include <queue>
    #include <stack>
    #include <map>
    #include <set>
    #include <vector>
    #include <iomanip>
    #define ALL(x) (x).begin(), (x).end()
    #define rt return
    #define dll(x) scanf("%I64d",&x)
    #define xll(x) printf("%I64d
    ",x)
    #define sz(a) int(a.size())
    #define all(a) a.begin(), a.end()
    #define rep(i,x,n) for(int i=x;i<n;i++)
    #define repd(i,x,n) for(int i=x;i<=n;i++)
    #define pii pair<int,int>
    #define pll pair<long long ,long long>
    #define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
    #define MS0(X) memset((X), 0, sizeof((X)))
    #define MSC0(X) memset((X), '', sizeof((X)))
    #define pb push_back
    #define mp make_pair
    #define fi first
    #define se second
    #define eps 1e-6
    #define gg(x) getInt(&x)
    #define db(x) cout<<"== [ "<<x<<" ] =="<<endl;
    using namespace std;
    typedef long long ll;
    ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
    ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
    ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
    inline void getInt(int* p);
    const int maxn=1000010;
    const int inf=0x3f3f3f3f;
    /*** TEMPLATE CODE * * STARTS HERE ***/
    ll h,w,n;
    int main()
    {
        //freopen("D:\common_text\code_stream\in.txt","r",stdin);
        //freopen("D:\common_text\code_stream\out.txt","w",stdout);
        gbtb;
        cin>>h>>w>>n;
        ll cnt=0ll;
        int flag=1;
        repd(i,1,h)
        {
            if(flag)
            {
                cnt+=(w+1)/2;
            }else
            {
                cnt+=(w-1)/2;
            }
            flag=!flag;
        }
        if(cnt>=n)
        {
            flag=1;
            repd(i,1,h)
            {
                if(flag)
                {
                    repd(j,1,w)
                    {
                        if(j&1)
                        {
                            if(n>0)
                            {
                                cout<<"#";
                                n--;
                            }else
                            {
                                cout<<"*";
                            }
    
                        }else
                        {
                            cout<<"*";
                        }
                    }
                    cout<<endl;
                }else
                {
                    repd(j,1,w)
                    {
                        if(j&1)
                        {
                            cout<<"*";
                        }else
                        {
                            if(n>0)
                            {
                                cout<<"#";
                                n--;
                            }else
                            {
                                cout<<"*";
                            }
    
                        }
                    }
                    cout<<endl;
                }
                flag=!flag;
            }
        }else
        {
            cout<<-1<<endl;
        }
    
        return 0;
    }
    
    inline void getInt(int* p) {
        char ch;
        do {
            ch = getchar();
        } while (ch == ' ' || ch == '
    ');
        if (ch == '-') {
            *p = -(getchar() - '0');
            while ((ch = getchar()) >= '0' && ch <= '9') {
                *p = *p * 10 - ch + '0';
            }
        }
        else {
            *p = ch - '0';
            while ((ch = getchar()) >= '0' && ch <= '9') {
                *p = *p * 10 + ch - '0';
            }
        }
    }
    
    

    本博客为本人原创,如需转载,请必须声明博客的源地址。 本人博客地址为:www.cnblogs.com/qieqiemin/ 希望所写的文章对您有帮助。
  • 相关阅读:
    即时通信 选择UDP还是TCP协议
    Gradle 在Eclipse中的使用
    使用idea+gradle建立SSM项目
    Gradle安装和在IDEA使用 基本操作
    IDEA配置 gradle
    Trustin Lee
    java.security.MessageDigest (2) 生成安全令牌!
    java.security.MessageDigest (1)
    递归算法结合数据库 解析 java树形结构
    mysql 日期加减
  • 原文地址:https://www.cnblogs.com/qieqiemin/p/10703056.html
Copyright © 2011-2022 走看看