USACO 3.1.4 Shaping Regions

Source: Internet
Author: User

Shaping RegionsN opaque rectangles (1 <= N <= 1000) of varous colors are placed on a white sheet of paper whose size is A wide by B long. the rectangles are put with their sides parallel to the sheet's borders. all rectangles fall within the borders of the sheet so that different figures of different colors will be seen. the coordinate system has its origin (0, 0) at the sheet's lower left corner wi Th axes parallel to the sheet's borders. program name: rect1INPUT FORMATThe order of the input lines dictates the order of laying down the rectangles. the first input line is a rectangle "on the bottom ". line 1: A, B, and N, space separated (1 <= A, B <= 10,000) Lines 2-N + 1: Five integers: llx, lly, urx, ury, color: the lower left coordinates and upper right coordinates of the rectangle whose color is 'Color' (1 <= color <= 2500) to be placed on the white sheet. the color 1 is the same color of white as the sheet upon which the rectangles are placed. sample input (file rect1.in) 20 20 32 2 18 20 8 19 38 0 10 19 4 INPUT EXPLANATIONNote that the rectangle delineated by and is two units wide and two high. here's a schematic divisor of the input: 11111111111111111111333333334433333333313 Comment '4's at 8, 0 to 10, 19 are only two wid E, not three (I. e ., the grid contains a 4 and 8, 0 and a 4 and 8, 1 but NOT a 4 and 8, 2 since this districan't capture what wocould be shown on graph paper ). OUTPUT FORMATThe output file shoshould contain a list of all the colors that can be seen along with the total area of each color that can be seen (even if the regions of color are disjoint ), ordered by increasing color. do not display colors with n O area. sample output (file rect1.out) 1 912 843 1874 38/* records */At the beginning, my idea was to open a two-dimensional array and read a new rectangle and update it once. However, the array of 10000*10000 is too large to be compiled. The second idea is to separate the rectangle according to the hint below. What I want is to read a rectangle and then check it with the previously read rectangle to see if there is overlap. If yes, split the new rectangle. However, to do this, you must open an array to track the data of each rectangle, because the rectangle is constantly changing, which is too troublesome to write. Therefore, we can only turn to nocow and find that we can reverse the previous idea. From the back to the front, because the rectangle behind it is what we see. The front rectangle is split when it is constantly colliding with the back rectangle until we finally get the rectangle we see. In this process, the rectangle behind it will not change. This method is called Floating Method. We use Recursive Implementation. Program: [cpp]/* ID: zhaorui3 PROG: rect1 LANG: C ++ */# include <fstream> using namespace std; int min (int a, int B) {return (a <B? A: B);} int max (int a, int B) {return (a> B? A: B);} int x1 [1001], y1 [1001], x2 [1001], y2 [1001], color [1001] = {1 }; int cnt [2501], N; void cover (int lx, int ly, int rx, int ry, int c, int h) {if (lx = rx | ly = ry) return; // if (h> N) cnt [c] + = (ry-ly) jumps out when the area is 0) * (rx-lx); else {if (ly <y1 [h]) cover (min (lx, x2 [h]), ly, min (rx, x2 [h]), min (y1 [h], ry), c, h + 1); if (rx> x2 [h]) cover (max (x2 [h], lx), min (y2 [h], ly), rx, Min (y2 [h], ry), c, h + 1); if (ry> y2 [h]) cover (max (x1 [h], lx ), max (y2 [h], ly), max (rx, x1 [h]), ry, c, h + 1); if (lx <x1 [h]) cover (lx, max (y1 [h], ly), min (rx, x1 [h]), max (y1 [h], ry), c, h + 1) ;}} int main () {ifstream fin ("rect1.in"); ofstream fout ("rect1.out "); fin> x2 [0]> y2 [0]> N; for (int I = 1; I <= N; ++ I) fin> x1 [I]> y1 [I]> x2 [I]> y2 [I]> color [I]; cnt [color [N] + = (X2 [N]-x1 [N]) * (y2 [N]-y1 [N]); for (int I = N-1; I> = 0; I --) cover (x1 [I], y1 [I], x2 [I], y2 [I], color [I], I + 1 ); for (int I = 1; I <= 2500; I ++) if (cnt [I]) fout <I <''<cnt [I] <endl; return 0 ;}note that if we split the rectangle, there may be duplicates. There are duplicates in the lower left of rectangle 1. If the intersection is divided according to the two intersections, the middle part of the intersection is repeated. Therefore, we need to determine the division method in advance. Here, we agree to divide the upper boundary of the rectangle on the current side of the boundary above the boundary according to the preceding division. We use the method of 1. In this case, if the bottom rectangle contains 2, hand it over to the case where the right boundary is greater than the top. To avoid duplication.

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.