POJ1691-Painting a board

Source: Internet
Author: User

Reprinted please indicate the source: Thank you

Http://blog.csdn.net/lyy289065406/article/details/6727035

 

General question:

There is a blackboard on the wall, which is divided into multiple rectangles. Each rectangle must be painted with a default color C.

During coloring, the pigment will flow down. To avoid mixing the color of the bottom rectangle with the pigment flowing down from the top, it is required that the color of the rectangle isDirectly adjacent to the topAll the rectangles must be filled with color.

When color a is filled, if the rectangle with the default color a has been colored, or does not meet the color requirements for the moment, replace the New brush and fill in color B.

 

Note:

1. After the rectangle I is colored, it is found that the preset color of the rectangle J under the rectangle I is the same as that of the rectangle I, and all the rectangles above the rectangle J are colored, if J meets the filling conditions, you can use the I-filled brush to fill J, instead of replacing the new brush.

2. If color a has been filled in before and color B has been filled in later, you need to re-fill color a, or enable the new brush. You cannot use the brush that was previously used to fill in color.

3. If color a has been filled in just now and you want to continue painting color a, you do not need to change the new brush.

4. The rectangle coloring cannot only color a part. After the rectangle I is determined to be colored, the entire area of the rectangle I will be colored.

 

First, pay attention to the input data. The input sequence of each rectangle is Y x C, not x y x y c.

If the x y coordinate is reversed, it will not be AC .....

 

Topology + DFS

 

The method is very intuitive. Each rectangle is regarded as a vertex. The I-degree of the rectangle at the top of the blackboard is 0, and then starts from the rectangle I, and is directly connected to the adjacent rectangle below it, the rectangle has an inbound value of + 1. In other words, the number of adjacent rectangles above the rectangle A is upnum, which is the entry level of the rectangle A (point.

After the rectangle I is colored, the incoming degrees of all adjacent rectangles under the rectangle I are-1.

If the input degree of a rectangle is 0, it is the color to be painted. If the input degree is not 0, coloring is not allowed.

Then, according to the coloring restrictions required by the subject, the DFS coloring scheme has a small amount of data and can be AC without pruning.

 

Finally, let's talk about how to determine that rectangle A is above rectangle B.

There are three basic positional relationships between rectangle A and rectangle B, for example:

Set the coordinates in the upper left corner of the rectangle to (LX, ly) the coordinates in the lower right corner of the rectangle to (RX, ry)

First, Judge rect [A]. ry = rect [B]. ly to determine whether the bottom of the rectangle A and the top of the rectangle B may overlap (directly adjacent)

Then we can determine three situations:

Case 1:

Rect [A]. Lx> = rect [B]. Lx & rect [A]. Lx <rect [B]. Rx

Case 2:

Rect [A]. Lx <= rect [B]. Lx & rect [A]. Rx> = rect [B]. Rx

Case 3:

Rect [A]. Rx> rect [B]. Lx & rect [A]. Rx <= rect [B]. Rx

 

Note that restrictions must be imposed on both the left and right directions. Other special cases are covered by these three relationships.

 

Source correction:

Tehr 1999

Http://code.google.com/p/djudge-core/downloads/detail? Name=tehran-1999.7z & can = 2 & Q =

 

 

 

// Memory time // 260 K 0 Ms # include <iostream> using namespace STD; struct rectangle {int lx, Ly; // coordinate int RX, Ry in the upper left corner; // The coordinate int setcolor in the lower right corner; // specifies whether the current rectangle is colored with int upnum; // The number of rectangles above the rectangle (upnum = 0 indicates that all the rectangles above the rectangle have been colored and the rectangle is to be colored) int low [16]; // point to the int PL below the rectangle; // low [] pointer}; Class info {public: Info (INT n = 0): n (n) {memset (color, false, sizeof (color); rect = new rectangle [n + 1]; initial (); minbrushnum = 20; // up to 15 rectangular DFS (0, 0, 1); // C = 0 indicates that currently there is no requirement for the color to be filled }~ Info () {cout <minbrushnum <Endl; Delete [] rect;} void initial (void); bool judge_upper (int A, int B ); // determine whether rectangle A is above rectangle B void DFS (int n, int C, int B); // n: number of currently colored rectangles, C: Current coloring, b: We are currently using the brush protected: int N; // Number of rectangles rectangle * rect; // bool color of N rectangles [21]; // mark the color int minbrushnum; // minimum number of brushes}; void info: initial (void) {for (int K = 1; k <= N; k ++) {CIN> rect [K]. LY> rect [K]. lx; CIN> rect [K]. ry> rect [K]. RX; CIN> rect [K]. setcolor; R ECT [K]. flag = false; rect [K]. upnum = 0; rect [K]. PL = 0; color [rect [K]. setcolor] = true;} For (INT I = 1; I <n; I ++) for (Int J = I + 1; j <= N; j ++) {If (! Judge_upper (I, j) // If the rectangle I is not above the rectangle J, judge_upper (J, I); // determines whether the rectangle J is above the rectangle I} return ;} bool info: judge_upper (int A, int B) {If (rect [A]. ry = rect [B]. ly) {If (rect [A]. lx> = rect [B]. lx & rect [A]. lx <rect [B]. RX) | // case 1 (rect [A]. lx <= rect [B]. lx & rect [A]. RX> = rect [B]. RX) | // case 2 (rect [A]. RX> rect [B]. lx & rect [A]. RX <= rect [B]. RX) // case 3 {rect [B]. upnum ++; rect [A]. low [rect [A]. PL ++] = B; return true;} return false;} void info :: DFS (int n, int C, int B) {If (n = N) {If (minbrushnum> B) minbrushnum = B; return;} If (C = 0) // when the color is set to random, the unpainted rectangles above the enumeration are all colored. {for (INT I = 1; I <= N; I ++) {If (! Rect [I]. flag & rect [I]. upnum = 0) {Int J; rect [I]. flag = true; For (j = 0; j <rect [I]. PL; j ++) // The upnum of the rectangle under the rectangle I-1 rect [rect [I]. low [J]. upnum --; DFS (n + 1, rect [I]. setcolor, B); // if the color of the next fill is the same as that of the current fill, use the same brush rect [I]. flag = false; For (j = 0; j <rect [I]. PL; j ++) rect [rect [I]. low [J]. upnum ++ ;}} else // fill in the color of the last filled color {bool tag = false; For (INT I = 1; I <= N; I ++) {If (rect [I]. setcolor = C &&! Rect [I]. flag & rect [I]. upnum = 0) {Int J; tag = true; rect [I]. flag = true; For (j = 0; j <rect [I]. PL; j ++) // The upnum of the rectangle under the rectangle I-1 rect [rect [I]. low [J]. upnum --; DFS (n + 1, C, B); // the color of the next fill is the same as the current time, use the same brush rect [I]. flag = false; For (j = 0; j <rect [I]. PL; j ++) rect [rect [I]. low [J]. upnum ++ ;}} if (! Tag) DFS (n, 0, B + 1); // The rectangle with the color C has been filled to meet the fill conditions, enable the new brush to fill in other colors} return ;} int main (void) {int case; CIN> case; For (INT c = 1; C <= case; C ++) {int N; CIN> N; info poj1691 (n);} return 0 ;}

 

Sample Input

8

7

0 0 2 2 1

0 2 1 6 2

2 0 4 2 1

1 2 4 4 2

1 4 3 6 1

4 0 6 4 1

3 4 6 6 2

14

0 0 3 2 3

0 2 2 4 1

0 4 1 8 4

1 4 2 7 2

1 7 3 8 1

2 2 3 7 4

3 4 7 6 3

3 6 7 8 4

5 0 8 3 3

5 3 6 4 2

8 0 9 3 2

6 3 9 4 1

7 4 9 8 2

3 0 5 4 4

4

0 0 3 1 1

0 1 2 5 2

0 5 3 6 1

2 1 3 5 1

13

0 0 3 3 1

0 3 2 4 2

0 4 2 5 1

2 3 5 4 1

2 4 3 5 3

3 4 6 5 2

3 2 4 3 1

3 0 4 2 2

4 0 8 1 3

4 1 5 3 2

5 1 6 4 3

6 1 8 3 1

6 3 8 5 1

13

0 0 3 3 1

0 3 2 4 2

0 4 2 5 1

2 3 5 4 1

2 4 3 5 2

3 4 6 5 2

3 2 4 3 1

3 0 4 2 2

4 0 8 1 3

4 1 5 3 2

5 1 6 4 3

6 1 8 3 1

6 3 8 5 1

6

0 0 2 3 9

2 0 3 3 1

3 0 4 3 9

4 0 5 3 1

5 0 6 3 9

6 0 7 3 1

8

0 0 2 3 9

0 3 2 5 1

2 0 3 2 1

2 2 3 4 1

2 4 3 5 2

3 0 4 3 1

3 3 4 5 2

4 0 5 5 3

12

0 0 8 3 9

0 3 2 9 1

2 3 4 5 1

2 5 4 8 1

2 8 4 9 1

4 3 6 4 2

4 4 6 7 2

4 7 5 9 1

5 7 7 9 1

6 3 8 5 1

6 5 8 7 1

7 7 8 9 1

Sample output

3

8

2

6

5

6

4

4

 

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.