581D

题目链接:点击打开链接(因为一些细节错了很多次,还是需要多加练习)
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 x1, y1, x2, y2, x3, y3 (1 ≤ x1, y1, x2, y2, x3, y3 ≤ 100), where xi and yi 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
AAAAA
BBBBB
BBBBB
CCCCC
CCCCC
input
4 4 2 6 4 2
output
6
BBBBBB
BBBBBB
AAAACC
AAAACC
AAAACC
AAAACC
题目解析:

This problem can be solved in many ways, let's consider one of them.

The first step is to calculate sum of squares s of given rectangles. Then the side of a answer square is sqrt(s). If sqrt(s) is not integer print -1. Else we need to make the following.

We brute the order in which we will add given rectangles in the answer square (we can do it with help of next_permutation()) and for every order we brute will we rotate current rectangle on 90 degrees or not (we can do it with help of bit masks). In the beginning on every iteration the answer square c in which we add the rectangles is empty.

For every rectangle, which we add to the answer square we make the following — we need to find the uppermost and leftmost empty cell free in answer square c (recall that we also brute will we rotate the current rectangle on 90 degrees or not). Now we try to impose current rectangle in the answer square c and the top left corner must coinside with the cell free. If current rectangle fully placed in the answer square c and does not intersect with the some rectangle which has already been added, we need to fill by the required letter appropriate cells in the answer square c.

If no one of the conditions did not disrupted after we added all three rectangles and all answer square c is fully filled by letters we found answer and we neeed only to print the answer square c.

Else if we did not find answer after all iterations on the rectangles — print -1.

For random number of the rectangles k asymptotic behavior — O(k! * 2k * s) where s — the sum of squares of the given rectangles.

Also this problem with 3 rectangles can be solved with the analysis of the cases with asymptotic O(s) where s — the sum of squares of given rectangles.

参考代码:

#include 
#include 
#include 
#include 

using namespace std;

#define F first
#define S second
int targe[105][105];
pair a[3];

int fill_map(vector > &pv, int len){
    int color = 0;
    for(auto &c: pv){
        int i, j; ++color;
        int ok = 0;
        for(i = 0; i < len; ++i){ for(j = 0; j < len; ++j) if(!targe[i][j]) {ok = 1; break;}
            if(ok) break;
        }
        int a = c.S+i, b = c.F+j;
        if(a > len || b > len) return 0;
        for( ; i < a; ++i)
            for(int k = j; k < b; ++k)
             if(targe[i][k]) return 0; else targe[i][k] = color;
    }
    return 1;
}
int check(int len){
    for(int i = 0; i < len; ++i)
        for(int j = 0; j < len; ++j) if(!targe[i][j]) return 0;
    return 1;
}

int main()
{
    while(scanf("%d%d%d%d%d%d", &a[0].F, &a[0].S, &a[1].F, &a[1].S, &a[2].F, &a[2].S) != EOF){
        int square = a[0].F*a[0].S + a[1].F*a[1].S + a[2].F*a[2].S;
        double sizeLen = sqrt(square);  // sizeLen 表示目标正方形边的长度;
        //判断有没有可能组成正方形;
        if(sizeLen-(int)sizeLen > 1e7) {printf("-1\n"); continue;}
        else{
            int ok = 0;
            do{
                vector > pv;
                for(int mask = 0; mask < (1<<3); ++mask){  //bitmasks;
                    memset(targe, 0, sizeof(targe));  //准备染色;
                    pv.clear();
                    for(int i = 0; i < 3; ++i){
                        if(mask & (1<


你可能感兴趣的:(581D)