Usaco shaping regions solution report

Source: Internet
Author: User

It was just over two months since the last report was written. You are blessing. Hope the waiting will have a sweet end. God bless me.

This topic is stuck for a long time due to the difficulties mentioned later. The more disgusting the code is, the more disgusting the code is. You can directly move the above link.

The general idea of this question is that there are many squares, and the placed squares will overwrite the color of the previous squares, so that the area occupied by the last color shown in the statistics.

The prompt of the question indicates that the color information is saved according to the square, that is, the coordinates and colors in the lower left and upper right corner, rather than the description of the question. In this way, the time and space consumption will be greatly reduced.

There were few questions about designing geometric figures before, so the difficulties mainly focused on two points:

1. How to determine whether two rectangles overlap (InterSect ).

The following code is used to determine whether square (LL, LR, UL, ur, color) overlaps squares [J:

Int maxll, maxlr, minul, minur; maxll = max (LL, squares [J]. ll); maxlr = max (LR, squares [J]. lr); minul = min (UL, squares [J]. ul); minur = min (UR, squares [J]. UR); If (maxll> = minul | maxlr> = minur) // No overlap {continue ;}

Comparison Figure 1:

We can know that if point A is completely on the right or top of point B, the two rectangles do not overlap.

2. How to divide overlapping images into rectangles. Here we need to find a general expression method, and other intersection conditions (such as overlapping one or two corners of the four corners) can become special cases.

The Code is as follows:

addSquare(squares, Square(squares[j].ll, squares[j].lr, maxll, squares[j].ur, squares[j].color));addSquare(squares, Square(maxll, minur, minul, squares[j].ur, squares[j].color));addSquare(squares, Square(minul, squares[j].lr, squares[j].ul, squares[j].ur, squares[j].color));addSquare(squares, Square(maxll, squares[j].lr, minul, maxlr, squares[j].color));

The second parameter of the addsquare method is the newly added rectangle (separated by the Section not covered in the covered rectangle). There are four in total, which correspond to (1) in Figure 2, respectively ), (2), (3), (4 ). Of course, the original two rectangles must be marked as unavailable (my method) or deleted, and the other rectangle must be added completely.

This is a general case, and other intersection cases are special cases. The ratio 1 contains only (1) and (4), and the remaining (2) and (3) are degraded into a line. Therefore, all the rectangles after the intersection can be processed in this way. When adding the rectangle, you must determine whether it is deformed (the area is greater than 0 ).

The complete code is as follows:

/*ID: thestor1LANG: C++TASK: rect1*/#include <iostream>#include <cstdio>#include <cstring>#include <vector>#include <cassert>#include <list>#include <map>using namespace std;struct Square{int ll, lr, ul, ur;int color;int area;bool cut;Square(){}Square(int ll, int lr, int ul, int ur, int color){this->ll = ll;this->lr = lr;this->ul = ul;this->ur = ur;this->color = color;this->area = (ul - ll) * (ur - lr);this->cut = false;}};void addSquare(vector<Square> &squares, const Square& square){if(square.area != 0){squares.push_back(square);}}int main(){FILE *fin  = fopen ("rect1.in", "r");FILE *fout = fopen ("rect1.out", "w");//freopen("log.txt", "w", stdout);int a, b, n;fscanf(fin, "%d%d%d", &a, &b, &n);vector<Square> squares;squares.push_back(Square(0, 0, a, b, 1));for(int i = 0; i < n; ++i){int ll, lr, ul, ur, color;fscanf(fin, "%d%d%d%d%d", &ll, &lr, &ul, &ur, &color);Square square(ll, lr, ul, ur, color);for(unsigned int j = 0; j < squares.size(); ++j){if(!squares[j].cut){int maxll, maxlr, minul, minur;maxll = max(ll, squares[j].ll);maxlr = max(lr, squares[j].lr);minul = min(ul, squares[j].ul);minur = min(ur, squares[j].ur);if(maxll >= minul || maxlr >= minur){continue;}squares[j].cut = true;addSquare(squares, Square(squares[j].ll, squares[j].lr, maxll, squares[j].ur, squares[j].color));addSquare(squares, Square(maxll, minur, minul, squares[j].ur, squares[j].color));addSquare(squares, Square(minul, squares[j].lr, squares[j].ul, squares[j].ur, squares[j].color));addSquare(squares, Square(maxll, squares[j].lr, minul, maxlr, squares[j].color));}}addSquare(squares, square);}map<int, int> cntColors;for(unsigned int i = 0; i < squares.size(); ++i){if(!squares[i].cut){if(cntColors.find(squares[i].color) == cntColors.end()){cntColors[squares[i].color] = squares[i].area;}else{cntColors[squares[i].color] += squares[i].area;}}}for(map<int, int>::iterator iter = cntColors.begin(); iter != cntColors.end(); ++iter){fprintf(fout, "%d %d\n", iter->first, iter->second);}return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.