您的位置:首页 > 其它

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]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: