Minieye杯第十五届华中科技大学程序设计邀请赛网络赛 D.Grid
题目描述
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(1≤h,w≤200,1≤n≤10^9) 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
输出
#*#*#*#*#*
题目数据量不大,就可以暴力模拟,n最大不超过h*w的一半,然后依次输出即可:
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
int main()
{
int h,w,n;
cin>>h>>w>>n;
if(n>ceil(double(h)*double(w)/2)) cout<<-1;
else {
for(int i=1;i<=h;i++){
for(int j=1;j<=w;j++){
if (n==0) cout<<"*";
else if((i+j)%2==0) {n--;cout<<"#";}
else if((i+j)%2) {cout<<"*";}
}
puts("");
}
}
}