HDU 1264 Counting Squares(哈希表||离散化)
2015-08-19 21:23
246 查看
Counting Squares
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1505 Accepted Submission(s): 767
[align=left]Problem Description[/align]
Your input is a series of rectangles, one per line. Each rectangle is specified as two points(X,Y) that specify the opposite corners of a rectangle. All coordinates will be integers in the range 0 to 100. For example, the line
5 8 7 10
specifies the rectangle who's corners are(5,8),(7,8),(7,10),(5,10).
If drawn on graph paper, that rectangle would cover four squares. Your job is to count the number of unit(i.e.,1*1) squares that are covered by any one of the rectangles given as input. Any square covered by more than one rectangle should only be counted once.
[align=left]Input[/align]
The input format is a series of lines, each containing 4 integers. Four -1's are used to separate problems, and four -2's are used to end the last problem. Otherwise, the numbers are the x-ycoordinates of two points that are opposite
corners of a rectangle.
[align=left]Output[/align]
Your output should be the number of squares covered by each set of rectangles. Each number should be printed on a separate line.
[align=left]Sample Input[/align]
5 8 7 10 6 9 7 8 6 8 8 11 -1 -1 -1 -1 0 0 100 100 50 75 12 90 39 42 57 73 -2 -2 -2 -2
[align=left]Sample Output[/align]
8 10000#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; #define N 105 int ax,bx,ay,by,a; bool xy ; int i,j,cover; int main() { while(~scanf("%d%d%d%d",&ax,&ay,&bx,&by)) { if(ax==ay&&ax==bx&&ax==by&&(ax==-1||ax==-2)) { for(i=0,cover=0;i<N;i++) for(j=0;j<N;j++) cover+=xy[i][j]; printf("%d\n",cover); if(ax==-2) break; memset(xy,0,sizeof(xy)); } else { if(ax>bx) {a=ax;ax=bx;bx=a;} if(ay>by) {a=ay;ay=by;by=a;} for(i=ax;i<bx;i++) for(j=ay;j<by;j++) xy[i][j]=1; } } return 0; } /* #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; #define N 105 int ax,bx,ay,by,a; struct TPoint { int x1,x2,y1,y2; }ply[10*N]; int n,m,yans[10*N],xans[10*N]; bool xy[10*N][10*N]; int i,j,k,i1,i2,j1,j2,cover; int main() { n=0; m=0; while(~scanf("%d%d%d%d",&ax,&ay,&bx,&by)) { if(ax==ay&&ax==bx&&ax==by&&(ax==-1||ax==-2)) { yans[0]=-1; xans[0]=-1; sort(yans,yans+m+1); sort(xans,xans+m+1); memset(xy,0,sizeof(xy)); for(i=1;i<=n;i++) { for(j=1;j<=m;j++) if(xans[j]==ply[i].x1) {i1=j;break;} for(j=1;j<=m;j++) if(yans[j]==ply[i].y1) {j1=j;break;} for(j=1;j<=m;j++) if(xans[j]==ply[i].x2) {i2=j;break;} for(j=1;j<=m;j++) if(yans[j]==ply[i].y2) {j2=j;break;} for(j=i1;j<i2;j++) for(k=j1;k<j2;k++) xy[j][k]=1; } for(i=1,cover=0;i<=m;i++) for(j=1;j<=m;j++) cover+=xy[i][j]*(xans[i+1]-xans[i])*(yans[j+1]-yans[j]); printf("%d\n",cover); if(ax==-2) break; n=m=0; } else { if(ax>bx) {a=ax;ax=bx;bx=a;} if(ay>by) {a=ay;ay=by;by=a;} n++; ply .x1=ax; ply .y1=ay; ply .x2=bx; ply .y2=by; yans[++m]=ay; xans[m]=ax; yans[++m]=by; xans[m]=bx; } } return 0; }*/
转载自 http://blog.sina.com.cn/s/blog_732dd9320100su6q.html[/code]
相关文章推荐
- hdoj 1969 Pie
- 小比赛(CodeForces 560A,UVA 11040,CodeForces 550B,HDU 1856,UVA 1644,CodeForces 560B,HDU 3405,UVA 10820)
- java 设计类时考虑多线程安全
- Myeclipse 10 破解说明
- 使用VS2010连接MySQL
- 数据持久化 将数据写入到本地 文件管理
- USB引脚定义以及导线的颜色定义
- ACM 概率&&动态规划
- 四校联赛
- PHP:echo print var_dump()和print_r()的区别
- System.getProperty("user.dir"); 获得系统属性
- MyBatis 关联查询
- 位运算 实现加法
- 汇编世界当中过程的经典
- 相遇~~~~
- undefined reference to `pthread_create'的错误。
- 不用加减乘除做加法题目:写一个函数,求两个整数之和
- 活不出自己,哪有什么情?
- hdoj1951
- InnoSetup快速入门(四):创建桌面快捷方式并作为可选的附加任务