题目描述:
题目链接:Codeforces Round #487 (Div. 2) C
题面:
There are four kinds of flowers in the wood, Amaranths, Begonias, Centaureas and Dianthuses.
The wood can be represented by a rectangular grid of n rows and m 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 a, b, c and d 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 n and m 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 n and m under the constraints below, they are not given in the input.
Input
The first and only line of input contains four space-separated integers a, b, c and d (1≤a,b,c,d≤100) — the required number of connected components of Amaranths, Begonias, Centaureas and Dianthuses, respectively.
Output
In the first line, output two space-separated integers n and m (1≤n,m≤50) — the number of rows and the number of columns in the grid respectively.
Then output n lines each consisting of m 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).
题目大意:
如果字母相同的两个字母方块共边,则这两个方块属于一个联通块。现给出A、B、C、D的联通块的数量,要求构造一个长和宽<=50的矩阵,使得A、B、C、D的联通块数量分别等于给出的值,保证每种联通块的数量<=100。
样例输入输出:
input
5 3 2 1
output
4 7
DDDDDDD
DABACAD
DBABACD
DDDDDDD
input
50 50 1 1
output
4 50
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
ABABABABABABABABABABABABABABABABABABABABABABABABAB
BABABABABABABABABABABABABABABABABABABABABABABABABA
DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD
input
1 6 4 5
output
7 7
DDDDDDD
DDDBDBD
DDCDCDD
DBDADBD
DDCDCDD
DBDBDDD
DDDDDDD
题目分析:
一道构造题,关键在于题目说每种联通块的数量小于等于100,而我们最大能构造
50
∗
50
50*50
50∗50的矩阵。那么我们就构造
50
∗
50
50*50
50∗50的矩阵,然后均分成四块,即
25
∗
25
25*25
25∗25的矩阵,每个矩阵都先分别全涂成A,B,C,D。此时每种联通块数量为1,如果题目给出A的联通块数量为5,我们就在其他区域点三个孤立的点,这样A就满足要求了,而又不会改变其他的数量。为了方便,我们就点在处于对角线和它不共边的大区域。如图所示:
为了保证点的是孤立的块且原区域依然联通,那么每一行应该隔一个点一个,然后也是隔一行才点。这样对于
25
∗
25
25*25
25∗25的矩阵,每行能点13个,能点13行,总计
13
∗
13
>
100
13*13>100
13∗13>100。
以 25 25 25 25为例的构造结果:
附代码:
#include<iostream>
#include<cstring>
using namespace std;
int a,b,c,d,pox,poy;
char s[510][510];
int main()
{
// freopen("lx.in","r",stdin);
while(scanf("%d%d%d%d",&a,&b,&c,&d)!=EOF)
{
memset(s,0,sizeof(s));
for(int i=1;i<=50;i++)
for(int j=1;j<=50;j++)
{
if(i<=25&&j<=25) s[i][j]='A';
if(i<=25&&j>25) s[i][j]='B';
if(i>25&&j<=25) s[i][j]='C';
if(i>25&&j>25) s[i][j]='D';
}
pox=26;poy=26;
for(int i=1;i<a;i++)
{
s[pox][poy]='A';
poy+=2;
if(poy>50)
{
pox+=2;
poy=26;
}
}
pox=26;poy=1;
for(int i=1;i<b;i++)
{
s[pox][poy]='B';
poy+=2;
if(poy>25)
{
pox+=2;
poy=1;
}
}
pox=1;poy=26;
for(int i=1;i<c;i++)
{
s[pox][poy]='C';
poy+=2;
if(poy>50)
{
pox+=2;
poy=26;
}
}
pox=1;poy=1;
for(int i=1;i<d;i++)
{
s[pox][poy]='D';
poy+=2;
if(poy>25)
{
pox+=2;
poy=1;
}
}
printf("50 50\n");
for(int i=1;i<=50;i++)
{
for(int j=1;j<=50;j++) printf("%c",s[i][j]);
printf("\n");
}
}
return 0;
}