为了宣传信息竞赛,要在长方形的三合板上喷油漆来制作招牌。三合板上不需要涂色的部分预先贴好了护板。被护板隔开的区域要涂上不同的颜色,比如上图就应该涂上5种颜色。
请编写一个程序计算涂色数量,输入数据中,保证看板不会被护板全部遮住,并且护板的边一定是水平或垂直的。
输入:
第一个数是宽w(1 ≤ w ≤ 1000000),第二个数是高h(1 ≤ h ≤ 1000000)。
第二行是护板的数量n(1 ≤ n ≤ 1000),接着n行是每个护板的左下角坐标 (x1 , y1 )和右上角坐标 (x2 , y2 ),用空格隔开: x1 , y1 , x2 , y2 (0 ≤ x1< x2 ≤ w, 0 ≤ y1 < y2 ≤ h 都是整数)
招牌的坐标系如下,左下角是 (0, 0) ,右上角是(w, h) , 测试集中的30%都满足w ≤ 100, h ≤ 100, n ≤ 100。
输出:
一个整数,代表涂色数量。
输入输出的例子
15 6 10 1 4 5 6 2 1 4 5 1 0 5 1 6 1 7 5 7 5 9 6 7 0 9 2 9 1 10 5 11 0 14 1 12 1 13 5 11 5 14 6 0 0
5
直接用坐标离散化处理,其中用于记录是否被覆盖了得部分可以直接用暴力方法过,也可以用imos法,这个方法非常有意思,建议大家学学
纯暴力:
imos方法:/*?????*/ #include <map> #include <set> #include <cmath> #include <ctime> #include <queue> #include <vector> #include <cctype> #include <cstdio> #include <string> #include <cstring> #include <sstream> #include <cstdlib> #include <iomanip> #include <typeinfo> #include <iostream> #include <algorithm> #include <functional> using namespace std; #define pb push_back #define mp make_pair #define mem(a, x) memset(a, x, sizeof(a)) #define copy(a, b) memcpy(a, b, sizeof(a)) #define lson rt << 1, l, mid #define rson rt << 1|1, mid + 1, r #define FIN freopen("input.txt", "r", stdin) #define FOUT freopen("output.txt", "w", stdout) typedef long long LL; typedef pair<int, int > PII; typedef pair<int, string> PIS; typedef pair<LL, LL>PLL; typedef unsigned long long uLL; template<typename T> void print (T* p, T* q, string Gap = " ", bool flag = false) { int d = p < q ? 1 : -1; while (p != q) { if (flag) cout << Gap[0] << *p << Gap[1]; else cout << *p; p += d; if (p != q && !flag) cout << Gap; } cout << endl; } template<typename T> void print (const T &a, string bes = "") { int len = bes.length(); if (len >= 2) cout << bes[0] << a << bes[1] << endl; else cout << a << endl; } template<typename T> void debug (T* p, T* q, string Gap = " ", bool flag = false) { int d = p < q ? 1 : -1; cout << "Debug out:"; while (p != q) { if (flag) cout << Gap[0] << *p << Gap[1]; else cout << *p; p += d; if (p != q && !flag) cout << Gap; } cout << endl; } template<typename T> void debug (const T &a, string bes = "") { int len = bes.length(); cout << "Debug out:"; if (len >= 2) cout << bes[0] << a << bes[1] << endl; else cout << a << endl; } void IO_Init() { ios::sync_with_stdio (false); } LL LLabs (const LL a) { return a >= 0 ? a : -a; } const double PI = 3.1415926535898; const double eps = 1e-10; const int MAXM = 1e5 + 5; const int MAXN = 1e3 + 5; const int INF = 0x3f3f3f3f; /*?????*/ int W, H, N; int X1[MAXN], X2[MAXN],Y1[MAXN], Y2[MAXN]; bool fld[MAXN * 6][MAXN * 6]; int dx[4] = {1,0,-1,0}; int dy[4] = {0,1,0,-1}; int compress(int *x1, int *x2, int w){ vector<int>xs; for(int i = 0;i < N;i ++){ for(int d = -1;d <= 1;d ++){ int tx1 = x1[i] + d, tx2 = x2[i] + d; if(1 <= tx1 && tx1 <= w) xs.pb(tx1); if(1 <= tx2 && tx2 <= w) xs.pb(tx2); } } sort(xs.begin(), xs.end()); xs.erase(unique(xs.begin(), xs.end()), xs.end()); for(int i = 0;i < N;i ++){ x1[i] = find(xs.begin(), xs.end(), x1[i]) - xs.begin(); x2[i] = find(xs.begin(), xs.end(), x2[i]) - xs.begin(); } return xs.size(); } void solve(){ W = compress(X1, X2, W); H = compress(Y1, Y2, H); mem(fld, false); for(int i = 0;i < N;i ++){ for(int y = Y1[i];y <= Y2[i];y ++){ for(int x = X1[i]; x <= X2[i];x ++){ fld[y][x] = true; } } } int ans = 0; for(int y = 0;y < H;y ++){ for(int x = 0;x < W;x ++){ if(fld[y][x]) continue; ans ++; fld[y][x] = true; queue<PII> que; que.push(mp(x, y)); while(!que.empty()){ int sx = que.front().first, sy = que.front().second; que.pop(); for(int i = 0;i < 4;i ++){ int nx = sx + dx[i]; int ny = sy + dy[i]; if(nx < 0 || ny < 0 || nx >= W || ny >= H) continue; if(fld[ny][nx]) continue; que.push(mp(nx, ny)); fld[ny][nx] = true; } } } } printf("%d\n", ans); } int main() { #ifndef ONLINE_JUDGE //FIN; //FOUT; #endif while(~scanf("%d%d", &W, &H), W && H){ scanf("%d", &N); for(int i = 0;i < N;i ++){ scanf("%d%d%d%d", &X1[i], &Y1[i], &X2[i], &Y2[i]); X1[i] ++, Y1[i] ++; } solve(); } return 0; }
/*?????*/ #include <map> #include <set> #include <cmath> #include <ctime> #include <queue> #include <vector> #include <cctype> #include <cstdio> #include <string> #include <cstring> #include <sstream> #include <cstdlib> #include <iomanip> #include <typeinfo> #include <iostream> #include <algorithm> #include <functional> using namespace std; #define pb push_back #define mp make_pair #define mem(a, x) memset(a, x, sizeof(a)) #define copy(a, b) memcpy(a, b, sizeof(a)) #define lson rt << 1, l, mid #define rson rt << 1|1, mid + 1, r #define FIN freopen("input.txt", "r", stdin) #define FOUT freopen("output.txt", "w", stdout) typedef long long LL; typedef pair<int, int > PII; typedef pair<int, string> PIS; typedef pair<LL, LL>PLL; typedef unsigned long long uLL; template<typename T> void print (T* p, T* q, string Gap = " ", bool flag = false) { int d = p < q ? 1 : -1; while (p != q) { if (flag) cout << Gap[0] << *p << Gap[1]; else cout << *p; p += d; if (p != q && !flag) cout << Gap; } cout << endl; } template<typename T> void print (const T &a, string bes = "") { int len = bes.length(); if (len >= 2) cout << bes[0] << a << bes[1] << endl; else cout << a << endl; } template<typename T> void debug (T* p, T* q, string Gap = " ", bool flag = false) { int d = p < q ? 1 : -1; cout << "Debug out:"; while (p != q) { if (flag) cout << Gap[0] << *p << Gap[1]; else cout << *p; p += d; if (p != q && !flag) cout << Gap; } cout << endl; } template<typename T> void debug (const T &a, string bes = "") { int len = bes.length(); cout << "Debug out:"; if (len >= 2) cout << bes[0] << a << bes[1] << endl; else cout << a << endl; } void IO_Init() { ios::sync_with_stdio (false); } LL LLabs (const LL a) { return a >= 0 ? a : -a; } const double PI = 3.1415926535898; const double eps = 1e-10; const int MAXM = 1e5 + 5; const int MAXN = 1e3 + 5; const int INF = 0x3f3f3f3f; /*?????*/ int W, H, N; int X1[MAXN], X2[MAXN],Y1[MAXN], Y2[MAXN]; int fld[MAXN * 2][MAXN * 2]; int dx[4] = {1,0,-1,0}; int dy[4] = {0,1,0,-1}; int compress(int *x1, int *x2, int w) { vector<int>xs; for(int i = 0; i < N; i ++) { int tx1 = x1[i], tx2 = x2[i]; if(1 <= tx1 && tx1 < w) xs.pb(tx1); if(1 <= tx2 && tx2 < w) xs.pb(tx2); } xs.pb(0); xs.pb(w); sort(xs.begin(), xs.end()); xs.erase(unique(xs.begin(), xs.end()), xs.end()); for(int i = 0; i < N; i ++) { x1[i] = find(xs.begin(), xs.end(), x1[i]) - xs.begin(); x2[i] = find(xs.begin(), xs.end(), x2[i]) - xs.begin(); } return xs.size() - 1; } void solve() { W = compress(X1, X2, W); H = compress(Y1, Y2, H); mem(fld, 0); for(int i = 0; i < N; i ++) { fld[Y1[i]][X1[i]] ++; fld[Y2[i]][X2[i]] ++; fld[Y2[i]][X1[i]] --; fld[Y1[i]][X2[i]] --; } for(int i = 0; i < H; i ++) { for(int j = 1; j < W; j ++) { fld[i][j] += fld[i][j - 1]; } } for(int i = 1; i < H; i ++) { for(int j = 0; j < W; j ++) { fld[i][j] += fld[i - 1][j]; } } int ans = 0; for(int y = 0; y < H; y ++) { for(int x = 0; x < W; x ++) { if(fld[y][x]) continue; ans ++; queue<PII> que; que.push(mp(x, y)); while(!que.empty()) { int sx = que.front().first, sy = que.front().second; que.pop(); for(int i = 0; i < 4; i ++) { int nx = sx + dx[i]; int ny = sy + dy[i]; if(nx < 0 || ny < 0 || nx > W || ny > H) continue; if(fld[ny][nx]) continue; que.push(mp(nx, ny)); fld[ny][nx] = 1; } } } } printf("%d\n", ans); } int main() { #ifndef ONLINE_JUDGE //FIN; //FOUT; #endif while(~scanf("%d%d", &W, &H), W && H) { scanf("%d", &N); for(int i = 0; i < N; i ++) { scanf("%d%d%d%d", &X1[i], &Y1[i], &X2[i], &Y2[i]); } solve(); } return 0; }