链接: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.
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(1≤h,w≤200,1≤n≤109)(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
输出
#*#*#*#*#* 题意:
给你一个高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\n",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), '\0', 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=;while(b){if(b%)ans=ans*a%MOD;a=a*a%MOD;b/=;}return ans;}
inline void getInt(int* p);
const int maxn=;
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=;
repd(i,,h)
{
if(flag)
{
cnt+=(w+)/;
}else
{
cnt+=(w-)/;
}
flag=!flag;
}
if(cnt>=n)
{
flag=;
repd(i,,h)
{
if(flag)
{
repd(j,,w)
{
if(j&)
{
if(n>)
{
cout<<"#";
n--;
}else
{
cout<<"*";
} }else
{
cout<<"*";
}
}
cout<<endl;
}else
{
repd(j,,w)
{
if(j&)
{
cout<<"*";
}else
{
if(n>)
{
cout<<"#";
n--;
}else
{
cout<<"*";
} }
}
cout<<endl;
}
flag=!flag;
}
}else
{
cout<<-<<endl;
} return ;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '');
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * - ch + '';
}
}
else {
*p = ch - '';
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * + ch - '';
}
}
}