Codeforces 989C (构造)
题面:
"I've been here once," Mino exclaims with delight, "it's breathtakingly amazing."
"What is it like?"
"Look, Kanno, you've got your paintbrush, and I've got my words. Have a try, shall we?"
There are four kinds of flowers in the wood, Amaranths, Begonias, Centaureas and Dianthuses.
The wood can be represented by a rectangular grid of nn rows and mm columns. In each cell of the grid, there is exactly one type of flowers.
According to Mino, the numbers of connected components formed by each kind of flowers are aa, bb, cc and dd respectively. Two cells are considered in the same connected component if and only if a path exists between them that moves between cells sharing common edges and passes only through cells containing the same flowers.
You are to help Kanno depict such a grid of flowers, with nn and mm arbitrarily chosen under the constraints given below. It can be shown that at least one solution exists under the constraints of this problem.
Note that you can choose arbitrary nn and mm under the constraints below, they are not given in the input.
The first and only line of input contains four space-separated integers aa, bb, cc and dd (1≤a,b,c,d≤1001≤a,b,c,d≤100) — the required number of connected components of Amaranths, Begonias, Centaureas and Dianthuses, respectively.
In the first line, output two space-separated integers nn and mm (1≤n,m≤501≤n,m≤50) — the number of rows and the number of columns in the grid respectively.
Then output nn lines each consisting of mm consecutive English letters, representing one row of the grid. Each letter should be among 'A', 'B', 'C' and 'D', representing Amaranths, Begonias, Centaureas and Dianthuses, respectively.
In case there are multiple solutions, print any. You can output each letter in either case (upper or lower).
5 3 2 1
4 7 DDDDDDD DABACAD DBABACD DDDDDDD
50 50 1 1
4 50 CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC ABABABABABABABABABABABABABABABABABABABABABABABABAB BABABABABABABABABABABABABABABABABABABABABABABABABA DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD
1 6 4 5
7 7 DDDDDDD DDDBDBD DDCDCDD DBDADBD DDCDCDD DBDBDDD DDDDDDD
In the first example, each cell of Amaranths, Begonias and Centaureas forms a connected component, while all the Dianthuses form one.
题目描述:
给你四个数a,b,c,d,分别代表四个字符A,B,C,D,的连通分量的个数,让你去构造出最大为50*50的一个符合条件的图。
题目分析:
我们可以考虑到如下的策略。首先我们可以确定将整张图初始化为两种字母,使得这两种字母的连通分量分别为。
如n,m均为6时,可以先构造出:
A | A | A | A | A | A |
A | A | A | A | A | A |
A | A | A | A | A | A |
B | B | B | B | B | B |
B | B | B | B | B | B |
B | B | B | B | B | B |
这样的一个矩阵。
其次,我们可以这么考虑,我们要使得C和D的连通分量增加,则我们就需要在图中的A更新成C或B,在原来图中的B更新成A或D。而要使得上下两块(A和B)的连通性不发生改变,我们就必须要间隔地进行更改,比如上述的图原来为.,我们要增加C的连通分量而使得原来A的不发生改变,则我们就需要将AAAA....变成CACA....即可,即变成下面的表格:
C | A | C | A | C | A |
C | A | C | A | C | A |
A | A | A | A | A | A |
此时A的连通分量不会发生改变,同理B也进行如下操作即可。
而因为最大题目所给的a,b,c,d,均不知道范围,因此我们就贪心地取最大就50即可。
代码:
#include <bits/stdc++.h>
using namespace std;
char str[60][60];
int main()
{
int a,b,c,d;
scanf("%d%d%d%d",&a,&b,&c,&d);
for(int i=0;i<50;i++){
for(int j=0;j<50;j++){
if(i>25){
str[i][j]='B';
}
else str[i][j]='A';
}
}
a--,b--;
for(int i=0;i<25;i+=2){
for(int j=0;j<50;j+=2){
if(b){
str[i][j]='B';
b--;
}
else if(c){
str[i][j]='C';
c--;
}
}
}
for(int i=27;i<50;i+=2){
for(int j=0;j<50;j+=2){
if(a){
str[i][j]='A';
a--;
}
else if(d){
str[i][j]='D';
d--;
}
}
}
puts("50 50");
for(int i=0;i<50;i++){
puts(str[i]);
}
return 0;
}