codeforces #322 div 2 D. Three Logos (枚举)

D. Three Logos

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Three companies decided to order a billboard with pictures of their logos. A billboard is a big square board. A logo of each company is a rectangle of a non-zero area.

Advertisers will put up the ad only if it is possible to place all three logos on the billboard so that they do not overlap and the billboard has no empty space left. When you put a logo on the billboard, you should rotate it so that the sides were parallel to the sides of the billboard.

Your task is to determine if it is possible to put the logos of all the three companies on some square billboard without breaking any of the described rules.

Input

The first line of the input contains six positive integers _x_1, _y_1, _x_2, _y_2, _x_3, _y_3 (1 ≤ _x_1, _y_1, _x_2, _y_2, _x_3, _y_3 ≤ 100), where x__i and y__i determine the length and width of the logo of the i-th company respectively.

Output

If it is impossible to place all the three logos on a square shield, print a single integer “-1” (without the quotes).

If it is possible, print in the first line the length of a side of square n, where you can place all the three logos. Each of the next n lines should contain n uppercase English letters “A”, “B” or “C”. The sets of the same letters should form solid rectangles, provided that:

  • the sizes of the rectangle composed from letters “A” should be equal to the sizes of the logo of the first company,
  • the sizes of the rectangle composed from letters “B” should be equal to the sizes of the logo of the second company,
  • the sizes of the rectangle composed from letters “C” should be equal to the sizes of the logo of the third company,

Note that the logos of the companies can be rotated for printing on the billboard. The billboard mustn’t have any empty space. If a square billboard can be filled with the logos in multiple ways, you are allowed to print any of them.

See the samples to better understand the statement.

Sample test(s)

input

5 1 2 5 5 2

output

5<br></br>AAAAA<br></br>BBBBB<br></br>BBBBB<br></br>CCCCC<br></br>CCCCC

input

4 4 2 6 4 2

output

6<br></br>BBBBBB<br></br>BBBBBB<br></br>AAAACC<br></br>AAAACC<br></br>AAAACC<br></br>AAAACC<br></br><br></br>题意很清楚.<br></br>给三个矩形,问能否拼成一个正方形.<br></br>我们先把矩形都躺着放(保证x<=y)<br></br>然后容易知道,一共有两种可能的方法<br></br>一种是三个矩形罗在一起<br></br>另外一种是一个矩形在上面,下面两个矩形竖着放.<br></br>后一种方法又因为上面的矩形的不同以及下面两个矩形的横竖放置不同(00 01 10 11)*3<br></br>所以一共有12种要枚举...<br></br>直接写会累死...<br></br>想了个简单点的办法,具体见代码.<br></br>一遍AC,好爽!!!

  1/*************************************************************************
  2	> File Name: code/cf/#322/D.cpp
  3	> Author: 111qqz
  4	> Email: rkz2013@126.com 
  5	> Created Time: 2015年09月30日 星期三 17时21分24秒
  6 ************************************************************************/
  7#include<iostream>
  8#include<iomanip>
  9#include<cstdio>
 10#include<algorithm>
 11#include<cmath>
 12#include<cstring>
 13#include<string>
 14#include<map>
 15#include<set>
 16#include<queue>
 17#include<vector>
 18#include<stack>
 19#include<cctype>
 20#define y1 hust111qqz
 21#define yn hez111qqz
 22#define j1 cute111qqz
 23#define ms(a,x) memset(a,x,sizeof(a))
 24#define lr dying111qqz
 25using namespace std;
 26#define For(i, n) for (int i=0;i<int(n);++i)  
 27typedef long long LL;
 28typedef double DB;
 29const int inf = 0x3f3f3f3f;
 30int ans;
 31struct Point
 32{
 33    int x,y;
 34}p[10];
 35char maze[110][110];
 36int ss;
 37int lst;
 38int a[10],b[10];
 39bool solve()
 40{
 41    if (lst==-1) return false;
 42 //   cout<<"lst:"<<lst<<endl;
 43    if (p[0].x+p[1].x+p[2].x==p[0].y&&p[0].y==p[1].y&&p[0].y==p[2].y)
 44    {
 45	ans = p[0].y;
 46	for ( int i = 0 ; i < p[0].x ; i++)
 47	{
 48	    for ( int j = 0 ; j < ans ; j++)
 49	    {
 50		maze[i][j] = 'A';
 51	    }
 52	}
 53	for ( int i = p[0].x ; i < p[0].x+p[1].x;i++)
 54	{
 55	    for ( int j = 0 ; j < ans ; j++)
 56	    {
 57		maze[i][j] = 'B';
 58	    }
 59	}
 60	for ( int i = p[0].x + p[1].x ; i < ans ; i++)
 61	{
 62	    for ( int j = 0 ; j < ans ; j++)
 63	    {
 64		maze[i][j] = 'C';
 65	    }
 66	}
 67	return true;
 68    }
 69
 70    if (p[a[lst]].x+p[b[lst]].x==p[lst].y&&p[a[lst]].y==p[b[lst]].y&&p[a[lst]].y+p[lst].x==p[lst].y)
 71    {
 72	ans = p[lst].y;
 73	for ( int i = 0 ; i < p[lst].x ; i++)
 74	{
 75	    for ( int j =  0  ;  j < ans ; j++)
 76	    {
 77		maze[i][j] = char(lst+'A');
 78	    }
 79	}
 80	for ( int i = p[lst].x ; i < ans ; i++)
 81	{
 82	    for ( int j = 0  ;  j < ans  ;j++)
 83	    {
 84		if (j<p[a[lst]].x)
 85		{
 86		    maze[i][j]=char(a[lst]+'A');
 87		   // cout<<i<<" "<<j<<" "<<char(a[lst]+'A')<<endl;
 88		}
 89		else
 90		{
 91		    maze[i][j] = char (b[lst] + 'A');
 92		}
 93	    }
 94	}
 95	return true;
 96    }
 97
 98
 99    if (p[a[lst]].x+ p[b[lst]].y==p[lst].y&&p[a[lst]].y==p[b[lst]].x&&p[a[lst]].y+p[lst].x==p[lst].y)
100    {
101	//cout<<"22222222222222222"<<endl;
102	ans = p[lst].y;
103	for ( int i = 0 ; i < p[lst].x ; i++)
104	{
105	    for ( int j = 0 ;  j < ans ; j ++)
106	    {
107		maze[i][j] = char(lst+'A');
108	    }
109	}
110
111	for ( int i = p[lst].x ; i < ans ; i++)
112	{
113	    for ( int j = 0 ; j < ans ; j++)
114	    {
115		if (j<p[a[lst]].x)
116		{
117		    maze[i][j] =char(a[lst]+'A');
118		}
119		else
120		{
121		    maze[i][j] = char(b[lst]+'A');
122		}
123	    }
124	}
125	return true;
126    }
127if (p[a[lst]].y+p[b[lst]].x==p[lst].y&&p[a[lst]].x==p[b[lst]].y&&p[a[lst]].x+p[lst].x==p[lst].y)
128    {
129	//cout<<"3333333333333333333333"<<endl;
130	ans = p[lst].y;
131	for ( int i = 0 ; i < p[lst].x ; i++)
132	{
133	    for ( int j =  0  ;  j < ans ; j++)
134	    {
135		maze[i][j] = char(lst+'A');
136	    }
137	}
138	for ( int i = p[lst].x ; i < ans ; i++)
139	{
140	    for ( int j = 0  ;  j < ans  ;j++)
141	    {
142		if (j<p[a[lst]].y)
143		{
144		    maze[i][j]=char(a[lst]+'A');
145		}
146		else
147		{
148		    maze[i][j] = char (b[lst] + 'A');
149		}
150	    }
151	}
152	return true;
153    }
154if (p[a[lst]].y+p[b[lst]].y==p[lst].y&&p[a[lst]].x==p[b[lst]].x&&p[a[lst]].x+p[lst].x==p[lst].y)
155    {
156	//	cout<<"44444444444444444444444444"<<endl;
157	ans = p[lst].y;
158	for ( int i = 0 ; i < p[lst].x ; i++)
159	{
160	    for ( int j =  0  ;  j < ans ; j++)
161	    {
162		maze[i][j] = char(lst+'A');
163	    }
164	}
165	for ( int i = p[lst].x ; i < ans ; i++)
166	{
167	    for ( int j = 0  ;  j < ans  ;j++)
168	    {
169		if (j<p[a[lst]].y)
170		{
171		    maze[i][j]=char(a[lst]+'A');
172		 //   cout<<"haaa"<<a[lst]+'A'<<endl;
173		}
174		else
175		{
176		    maze[i][j] = char (b[lst] + 'A');
177		}
178	    }
179	}
180	return true;
181    }
182    return false;
183}
184int main()
185{
186  #ifndef  ONLINE_JUDGE 
187   freopen("in.txt","r",stdin);
188  #endif
189
190   ss = 0;
191   lst = -1;
192   a[0]=1;b[0]=2;
193   a[1]=0;b[1]=2;
194   a[2]=0;b[2]=1;
195   for ( int i = 0 ; i < 3 ; i++) 
196    {
197	scanf("%d %d",&p[i].x,&p[i].y);
198	if (p[i].x>p[i].y) swap(p[i].x,p[i].y);
199	ss += p[i].x*p[i].y;
200    }
201    for ( int i = 0 ; i < 3 ; i++)
202    {
203	if (p[i].y*p[i].y==ss)
204	{
205	    lst = i;
206	}
207    }
208    if (solve())
209    {
210	printf("%d\n",ans);
211	for ( int i = 0 ; i < ans ; i++)
212	{
213	    for ( int j = 0 ; j < ans ;  j++)
214	    {
215		printf("%c",maze[i][j]);
216	    }
217	    printf("\n");
218	}
219    }
220    else
221    {
222	puts("-1");
223    }
224 #ifndef ONLINE_JUDGE  
225
226    fclose(stdin);
227  #endif
228	return 0;
229}