Topic Link: Click to open the link
The main idea: a rectangle consisting of n small rectangles, now to the small rectangular dyeing, but the pigment will fall, in order to prevent the confusion of pigments, so to dye the above rectangle, and then dye the rectangle below, each time to change the color to use a new brush, ask the minimum number of brushes.
According to the dyeing conditions, you can find a topological sequence, the topological sequence before the first to dye, after the dye, in the order of the topology of Dfs to find the minimum number of brush words.
#include <cstdio> #include <cstring> #include <vector> #include <stack> #include <algorithm >using namespace std; struct node{int y1, x1, y2, x2, C;} P[20];vector <int> vec[20]; int sta[20], k; int in[20], min1, n; int vis[20]; int cmp (node A,node b) {return A.y1 < b.y1;} void Dfs (int c,int num,int ans) {if (ans > min1) {return; } if (num = = N) {min1 = ans; return; } int I, J, l; for (i = 0; i < n; i++) {if (Vis[i] | | in[i]) continue; if (p[i].c = = c) {Vis[i] = 1; L = vec[i].size (); for (j = 0; J < L; j + +) in[Vec[i][j]]--; sta[k++] = i; DFS (C,num+1,ans); k--; Vis[i] = 0; for (j = 0; J < L; j + +) in[Vec[i][j]]++; } else {vis[i] = 1; L = vec[i].size (); for (j = 0; J < L; j + +) in[Vec[i][j]]--; sta[k++] = i; DFS (P[I].C,NUM+1,ANS+1); k--; Vis[i] = 0; for (j = 0; J < L; j + +) in[Vec[i][j]]++; }}}int Main () {int T, I, J; scanf ("%d", &t); while (t--) {scanf ("%d", &n); memset (In,0,sizeof (in)); memset (vis,0,sizeof (VIS)); for (i = 0; i < n; i++) vec[i].clear (); for (i = 0; i < n; i++) {scanf ("%d%d%d%d%d", &p[i].y1, &p[i].x1, &p[i].y2, &P[I].X2, & AMP;P[I].C); } sort (p,p+n,cmp); for (i = 0, i < n; i++) {for (j = i+1; J < N; j + +) {if (P[i].y2 = = P[j].y1 && ! (p[j].x2 <= p[i].x1 | | p[j].x1 >= p[i].x2)) {Vec[i].push_back (j); in[j]++; }}} min1 = 100; for (i = 0; i < n; i++) {if (p[i].y1 ) break; DFS (p[i].c,0,0); } printf ("%d\n", min1+1); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Poj1691--painting A Board (topology +dfs)