AOJ 0531 Coordinate Discretization

Coloring: (Japanese title, translated into Chinese by myself) In order to promote the information contest, we need to spray paint on the rectangular plywood to make the signboard. The parts of the plywood that do not need to be painted are pre-attached to the guard plate. The area separated by the guard plate should be painted with different colors, for example, the above picture should be painted with 5 colors.

Please write a program to calculate the number of colors to be painted, and input the data to ensure that the kanban board is not completely covered by the guard board, and the side of the guard board must be horizontal or vertical.

enter:

The first number is the width w (1 ≤ w ≤ 1000000) and the second number is the height h (1 ≤ h ≤ 1000000).

The second line is the number n of panels (1 ≤ n ≤ 1000), then the n lines are the coordinates of the lower left corner (x1 , y1 ) and the upper right corner (x2 , y2 ) of each panel, separated by spaces: x1 , y1 , x2 , y2 (0 ≤ x1< x2 ≤ w, 0 ≤ y1 < y2 ≤ h are all integers)

The coordinate system of the signboard is as follows, the lower left corner is (0, 0) , the upper right corner is (w, h) , and 30% of the test set satisfies w ≤ 100, h ≤ 100, n ≤ 100.

 

output:

An integer representing the number of colors to paint.

 

Solve using coordinate discretization.

 The idea of ​​coordinate discretization is: when the coordinate range is large and the number of coordinates is small, you can consider sorting all the abscissas used, and then use the subscript corresponding to each coordinate to update the coordinate position.

for example:

The horizontal coordinates used in this example are 1, 2, 4, 5, 6, 7, 9, 10, 11, 12, 13, 14.

Then the abscissa pair (1,5) can be converted to (0,3). One bit of space is saved, which will become more and more obvious as the gap becomes larger.

By discretizing the coordinates, the time complexity can usually be reduced to a satisfactory level.

The imos method is used here to further optimize the time complexity.

Next is the code, from http://www.hankcs.com/program/algorithm/aoj-0531-paint-color.html

  1 #include<iostream>
   2 #include<vector>
   3 #include<algorithm>
   4 #include<queue>
   5 #include <cstring>
   6  #define MAX_N 1000 + 16
   7   
  8  using  namespace std;
   9   
10  int N, H, W;
 11  int X1[MAX_N], X2[MAX_N], Y1[MAX_N], Y2[MAX_N];
 12  int fld[ 2 * MAX_N][ 2 * MAX_N], // used for filling traversal, representing coordinates (i, j ) is blank (after compression) 
13 dx[ 4 ] = { 1 , - 1, 0 , 0 }, dy[ 4 ] = { 0 , 0 , 1 , - 1 };
 14   
15  // Compress the coordinates, change the value of the coordinates into "this is the first kind of value", and return a total of several kinds of coordinates 
16  int compress( int *x1, int *x2, int w)
 17  {
 18      vector< int > xs;
 19   
20      for ( int i = 0 ; i < N; ++ i)
 21      {
 22          int tx1 = x1[i ], tx2 = x2[i];
 23         if (1 <= tx1 && tx1 < w) xs.push_back(tx1);
 24         if (1 <= tx2 && tx2 < w) xs.push_back(tx2);
 25     }
 26     xs.push_back(0);
 27     xs.push_back(w);
 28     sort(xs.begin(), xs.end());
 29     xs.erase(unique(xs.begin(), xs.end()), xs.end());
 30     for (int i = 0; i < N; ++i)
 31     {
 32         x1[i] = find(xs.begin(), xs.end(), x1[i]) - xs.begin();
 33         x2[i] = find(xs.begin(), xs.end(), x2[i]) - xs.begin();
 34     }
 35     return xs.size() - 1;
 36 }
 37  
 38 int bfs()
 39 {
 40     int ans = 0;
 41     for (int i = 0; i < H; ++i)
 42     {
 43         for (int j = 0; j < W; ++j)
 44         {
 45             if (fld[i][j]) continue;
 46             ++ans;
 47             queue<pair<int, int> >que;
 48             que.push(make_pair(j, i));
 49             while (!que.empty())
 50             {
 51                 int nx = que.front().first, ny = que.front().second;
 52                 que.pop();
 53  
 54                 for (int i = 0; i < 4; ++i)
 55                 {
 56                     int tx = nx + dx[i], ty = ny + dy[i];
 57                     if (tx < 0 || W < tx || ty < 0 || H< ty || fld[ty][tx] > 0) continue;
 58                     que.push(make_pair(tx, ty));
 59                     fld[ty][tx] = 1;
 60                 }
 61             }
 62         }
 63     }
 64     return ans;
 65 }
 66  
 67 ///////////////////////////SubMain//////////////////////////////////
 68 int main(int argc, char *argv[])
 69 {
 70     while (cin >> W >> H, W | H)
 71     {
 72         cin >> N;
 73         for (int i = 0; i < N; ++i)
 74         {
 75             cin >> X1[i] >> Y1[i] >> X2[i] >> Y2[i];
 76         }
 77  
 78         memset(fld, 0, sizeof(fld));
 79  
 80         W = compress(X1, X2, W);
 81         H = compress(Y1, Y2, H);
 82  
 83         // imos-法
 84         for (int i = 0; i < N; i++)
 85         {
 86             fld[Y1[i]][X1[i]]++;
 87             fld[Y1[i]][X2[i]]--;
 88             fld[Y2[i]][X1[i]]--;
 89             fld[Y2[i]][X2[i]]++;
 90         }
 91         // 横向累积
 92         for (int i = 0; i < H; i++)
 93         {
 94             for (int j = 1; j < W; j++)
 95             {
 96                 fld[i][j] += fld[i][j - 1];
 97             }
 98         }
 99         // 纵向累积
100         for (int i = 1; i < H; i++)
101         {
102             for (int j = 0; j < W; j++)
103             {
104                 fld[i][j] += fld[i - 1 ][j];
 105              }
 106          } // After accumulation, the non-0 part in fld indicates that there is a baffle 
107          cout << bfs() << endl;
 108      }
 109      return  0 ;
 110  }
 111  ///////////////////////// End Sub ///////////// //////////////////// _
View Code

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325339701&siteId=291194637