幻灯片-离散化入门

幻灯片
Time Limit : 3000/1000ms (Java/Other) Memory Limit : 65535/32768K (Java/Other)
Total Submission(s) : 3 Accepted Submission(s) : 2
Font: Times New Roman | Verdana | Georgia
Font Size: ← →

Problem Description

There are many colored slides in the plane.They are rectangles and semi-transparent.All slides are parallel with X-axis and Y-axis.The color of the slides can be labeled with an integer number.The slides may be overlapped.and the color of those slides may be mixed to make out a new color.The number of new color equals to the sum of the two colors overlapped.Your task is to find how many defferent colors appear in the plane.

Input

Input may consist of several test data sets.For each data set,it can be format as below:
Frist come one line with an integer N,1<=N<=100,the number of slides in the plane.Then N lines follow,each has five integer X1,Y1,X2,Y2,C,0<=X1,Y1,X2,Y2<=10^9,X1<X2,Y1<Y2,1<=C<=1000,representing the coordinate of the left-down corner of the slide,the coordinate of the right-up corner of the slide,and the number of the colors of the slide.
Input is ended with N=0.

Output

For each data set,simply output one integer in one line,representing the number of the different colors in the plane.

Sample Input

3
2 2 3 3 2 
2 0 4 4 1
1 1 3 5 3 
2 
1 1 2 2 3
2 2 3 3 3 
5
7 10 8 14 5
2 3 6 9 4
7 8 12 14 1
5 1 9 3 3
3 9 12 10 2
0

Sample Output

4
1
5

Author

HYNU
 
借助这道题,终于对离散化有了初步理解。
题意:许多 平行于x轴或y轴幻灯片放在平面上,每个幻灯片都有一个颜色用数字标识,这些幻灯片可以相互重叠,并且产生一种新颜色,用重叠的幻灯片的和来标识,问有多少种不同颜色的幻灯片出现在平面上。
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int MAXN=100;   //最大矩阵数
const int MAX_COLOR=100001;  //最大颜色编号
struct Rec               //矩阵的存储结构
{
    int x1,y1,x2,y2,c;
}r[MAXN];
int x[MAXN*2],xn;     //x坐标数值的数组,不同的x坐标总数
int y[MAXN*2],yn;     //y坐标数值的数组,不同的y坐标总数
int col[MAXN*2][MAXN*2];  //各个离散棋盘块的颜色
char mark[MAX_COLOR];     //标记某颜色编号是否存在
int main()
{
//     freopen("a.txt","r",stdin);
    int n, ans;   //矩阵数,答案
    int xl,xr,yl,yr;  //离散后的四个边界下标
    int i,j,k;
    while(scanf("%d",&n),n) //n=0时程序结束
    {
        for(i=0;i<n;i++)
        {
            scanf("%d%d%d%d%d",&r[i].x1,&r[i].y1,&r[i].x2,&r[i].y2,&r[i].c);
            //将坐标加入对应的数组中
            x[i*2]=r[i].x1, x[i*2+1]=r[i].x2;
            y[i*2]=r[i].y1, y[i*2+1]=r[i].y2;
        }
        //对x坐标数组排序并去重
        sort(x,x+n+n);
        for(xn=i=1;i<n+n;i++)
            if(x[i]!=x[i-1]) x[xn++]=x[i];
        //对y坐标数组排序并去重
        sort(y,y+n+n);
        for(yn=i=1;i<n+n;i++)
            if(y[i]!=y[i-1]) y[yn++]=y[i];

        memset(col,0,sizeof(col));
        for(i=0;i<n;i++)
        {
            //找出边界坐标对应离散后的坐标数组的下标
            for(j=0;j<xn;j++)
            {
                if(x[j]==r[i].x1) xl=j;  //找到x坐标的左边界下标
                if(x[j]==r[i].x2) xr=j;  //找到x坐标的右边界下标
            }
            for(j=0;j<yn;j++)
            {
                if(y[j]==r[i].y1) yl=j;  //找到y坐标的左边界下标
                if(y[j]==r[i].y2) yr=j;  //找到y坐标的右边界下标
            }
            //在离散后的棋盘坐标上染色
            for(j=xl;j<xr;j++)
                for(k=yl;k<yr;k++)
                    col[j][k]+=r[i].c;
        }
        //标记各种存在的颜色
        memset(mark,0,sizeof(mark));
        for(i=0;i<xn;i++)
            for(j=0;j<yn;j++)
                mark[col[i][j]]=1;
        for(ans=0,i=1;i<=MAX_COLOR;i++)
            ans+=mark[i];        //统计所有存在的颜色
        printf("%d\n",ans);      //输出答案
    }
    return 0;
}


 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值