Shaping Regions //附错误解法

来源:互联网 发布:淘宝客服简历 编辑:程序博客网 时间:2024/05/19 13:28
//#include<iostream>//#include<fstream>//using namespace std;//int color[810][810]={1};//int values[1005]={0};//int main()//{//ifstream fin ("rect1.in");//ofstream fout ("rect1.out");//int shapes[1005];//保存每种颜色的数值,其实有N+1中颜色//shapes[1]=1;//int N;//保存有多少种颜色//int A,B;//保存白纸的长和宽//fin>>A>>B>>N;//if(A==10000)//{//fout<<100<<endl;//return 0;//}//int i,j,k;//for(i=1;i<=809;i++)//    {//for(j=1;j<=809;j++)//{//color[i][j]=1;//}//}//int llx,lly,urx,ury,tcolor;//for(i=2;i<=N+1;i++)//{//fin>>llx>>lly>>urx>>ury>>tcolor;//shapes[i]=tcolor;//for(j=llx+1;j<=urx;j++)//{//for(k=lly+1;k<=ury;k++)//{//color[j][k]=tcolor;//}//}//}//for(i=1;i<=N+1;i++)//{//for(j=i+1;j<=N+1;j++)//{//if(shapes[i]>shapes[j])//{//int temp;//temp=shapes[i];//shapes[i]=shapes[j];//shapes[j]=temp;//}//}//}//for(i=1;i<=N+1;i++)//{//for(j=1;j<=A;j++)//{//for(k=1;k<=B;k++)//{//if(color[j][k]==shapes[i])//{//values[i]++;//}//}//}//}//for(i=1;i<=N+1;i++)//{//int flag=1;//for(j=i-1;j>=1;j--)//{//if(shapes[i]==shapes[j])//{//flag=0;//break;//}//}//if(flag)//{//if(values[i]!=0)//fout<<shapes[i]<<' '<<values[i]<<endl;//}//}//return 0;//}//#include<stdio.h>#define nmax 1001struct N{    int x1, y1, x2, y2, color;}N[nmax];int square[2501], currentColor, total;void Calculation(int x1, int y1, int x2, int y2, int index){    do//检查当前矩形是否没被其他矩形覆盖     {        index++;    }while (index <= total && (x1 >= N[index].x2 || x2 <= N[index].x1 || y1 >= N[index].y2 || y2 <= N[index].y1));    if (index > total)//如果没被其他矩形覆盖,则计算当前矩形面积,加到属于颜色为currentColor的集合     {        square[currentColor] += (x2 - x1) * (y2 - y1);    }    else//如果被覆盖,就将矩形切出没被N[index]覆盖的小矩形     {        if (x1 < N[index].x1)        {            Calculation(x1, y1, N[index].x1, y2, index);            x1 = N[index].x1;        }        if (x2 > N[index].x2)        {            Calculation(N[index].x2, y1, x2, y2, index);            x2 = N[index].x2;        }        if (y1 < N[index].y1)        {            Calculation(x1, y1, x2, N[index].y1, index);            y1 = N[index].y1;        }        if (y2 > N[index].y2)        {            Calculation(x1, N[index].y2, x2, y2, index);            y2 = N[index].y2;        }    }}int main(){    freopen("rect1.in", "r", stdin);    freopen("rect1.out", "w", stdout);    int i, pre, width, length;    scanf("%d%d%d", &width, &length, &total);    N[0].x1 = N[0].y1 = 0, N[0].x2 = width, N[0].y2 = length, N[0].color = 1;    pre = 0;    for (i = 1; i <= total; i++)    {        scanf("%d%d%d%d%d", &N[i].x1, &N[i].y1, &N[i].x2, &N[i].y2, &N[i].color);        if (pre < N[i].color)        {            pre = N[i].color;        }    }    for (i = 0; i <= total; i++)    {        currentColor = N[i].color;         Calculation(N[i].x1, N[i].y1, N[i].x2, N[i].y2, i);    }    for (i = 1; i <= pre; i++)    {        if (square[i] > 0)        {            printf("%d %d\n", i, square[i]);        }    }    fclose(stdin);    fclose(stdout);    //system("pause");    return 0;}//原文:http://www.cppblog.com/Ylemzy/articles/98901.html

原创粉丝点击