[UVALive 6663 Count the Regions] (dfs + 离散化)
来源:互联网 发布:量化交易程序员 招聘 编辑:程序博客网 时间:2024/06/05 04:12
链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4675
题目大意:
在一个平面上有 n (1<=n<=50) 个矩形,给你左上角和右下角的坐标(0<=x<=10^6, 0<=y<=10^6)。问这些矩形将该平面划分为多少块。
解题思路:
由于n很小,可以对整个图进行压缩,只要不改变每条边的相对位置,对答案没有影响。
可以将这些矩形的坐标离散化,然后把边上的点标记一下。之后进行简单dfs即可。(注意离散化的时候,两条边之间至少要隔一个距离)
代码:
/*ID: wuqi9395@126.comPROG:LANG: C++*/#include<map>#include<set>#include<queue>#include<stack>#include<cmath>#include<cstdio>#include<vector>#include<string>#include<fstream>#include<cstring>#include<ctype.h>#include<iostream>#include<algorithm>#define INF (1<<30)#define PI acos(-1.0)#define mem(a, b) memset(a, b, sizeof(a))#define For(i, n) for (int i = 0; i < n; i++)using namespace std;const int MOD = 1000000007;typedef long long ll;using namespace std;struct node { int a, b, c, d;} rec[600];int x[1200], y[1200];int xp[1000100], yp[1000100];int mp[240][240], n;int lx, ly;void gao() { for (int i = 0; i < n; i++) { int A = xp[rec[i].a]; int B = yp[rec[i].b]; int C = xp[rec[i].c]; int D = yp[rec[i].d]; for (int j = A; j <= C; j++) mp[j][D] = mp[j][B] = 1; for (int j = D; j <= B; j++) mp[A][j] = mp[C][j] = 1; }}int dir[4][2] = {{0, -1}, {0, 1}, {1, 0}, { -1, 0}};bool in(int x, int y) { return (x >= 0 && y >= 0 && x < 2 * lx + 1 && y < 2 * ly + 1);}void dfs(int x, int y) { mp[x][y] = 1; for (int i = 0; i < 4; i++) { int xx = x + dir[i][0]; int yy = y + dir[i][1]; if (in(xx, yy) && !mp[xx][yy]) { dfs(xx, yy); } }}int main() { while(scanf("%d", &n) != EOF && n) { for (int i = 0; i < n; i++) { scanf("%d%d%d%d", &rec[i].a, &rec[i].b, &rec[i].c, &rec[i].d); x[2 * i] = rec[i].a; x[2 * i + 1] = rec[i].c; y[2 * i] = rec[i].b; y[2 * i + 1] = rec[i].d; } sort(x, x + 2 * n); sort(y, y + 2 * n); lx = unique(x, x + 2 * n) - x; ly = unique(y, y + 2 * n) - y; for (int i = 0; i < lx; i++) { xp[x[i]] = 2 * i + 1; } for (int j = 0; j < ly; j++) { yp[y[j]] = 2 * j + 1; } memset(mp, 0, sizeof(mp)); gao(); int fk = 0; for (int i = 0; i < 2 * lx; i++) { for (int j = 0; j < 2 * ly; j++) if (mp[i][j] == 0) { dfs(i, j); fk++; } } cout << fk << endl; } return 0;}
0 0
- [UVALive 6663 Count the Regions] (dfs + 离散化)
- UvaLive 6663 Count the Regions 离散化+DFS
- UVALive 6663 Count the Regions (离散化,染色,dfs)
- UVALive 6663 Count the Regions --离散化+DFS染色
- UVALIve--6663--Count the Regions【离散化+搜索】
- UVALive 6663 Count the Regions 离散+bfs染色_(:зゝ∠)_
- LA 6663 —— Count the Regions(连通分量 + 离散化)
- UVALive6663 Count the Regions
- ZOJ 1652 Count the Regions
- UVALive 7661The Size of the Smallest Hole(dfs)
- hdu 4750 Count The Pairs (思维+并查集+离散化+二分查找)
- ZOJ 1610 Count the Colors(线段覆盖着色:离散化)
- Count the Colors 数颜色 离散化+线段树
- 离散化 usaco Shaping Regions (rect1)
- ACM-ICPC Regionals 2013 >> Asia - Aizu - Count the Regions
- CodeForcse 29C (DFS+离散化)
- HDU 5929 Coconuts(离散化+dfs)
- Coconuts----DFS+离散化
- hdu2177 取(2堆)石子游戏
- UVALive6663 Count the Regions
- 【VIM简明教程】
- hdu 1445 sticks (经典dfs+剪枝)
- java中try cathc finally return 相关异常问题
- [UVALive 6663 Count the Regions] (dfs + 离散化)
- 路由收敛
- 网络协议
- 分析USB平台设备模型框架(1)
- poj 1113 Wall 凸包模板题
- hdu 1083
- bootstrap之GetName&&GetAttribute&&GetDeviceSize&&GetSize&&GetLocation&&GetDataDir
- 百度员工离职总结:如何做个好员工?
- 物联网大赛有感