## CSDN博客

### ACM晚上的模拟赛也狂ＦＴ

附(ＰＫＵ１８９９）２００４．１０．１０的Ｆ题正确答案以及８小时人没找出来的３个字符的ＢＵＧ：

#include <iostream>
#include <fstream>
using namespace std;
int i,j;
bool findit(int up1,int down1,int left1,int right1,
int up2,int down2,int left2,int right2)
{
if(down2>=down1&&down2<=up1 && left2>=left1&&left2<=right1){
return true;
}
if(down2>=down1&&down2<=up1 && right2>=left1&&right2<=right1){
return true;
}
if(up2>=down1&&up2<=up1 && left2>=left1&&left2<=right1){
return true;
}
if(up2>=down1&&up2<=up1 && right2>=left1&&right2<=right1){
return true;
}
return false;
}

#include<fstream>
int main(void)
{
//  ifstream cin("test.txt");
int x,y,n;//1~1000xy,0~100n
int r[105][3];
int o[105][5];  //每行第0个表示是否有效
while(cin>>x>>y){
// input
cin>>n;
if(n == 0){
cout<<x*y<<"/n";
continue;
}

for(i=1;i<=n;i++){
cin>>r[i][0]>>r[i][1]>>r[i][2];
}
//
for(i=1;i<=n;i++){
o[i][0] = 0;
}
// cal
int up1,down1,left1,right1;
for(int i=1;i<=n;i++){
up1 = r[i][1]+r[i][2];
down1 = r[i][1]- r[i][2];
left1 = r[i][0] -r[i][2];
right1 = r[i][0] + r[i][2];
//取一圆看与里面的是否相交，相交就把里面的杀了
for(j=1;j<i;j++){
if(o[j][0] !=0 ){ //有效,取出判断
//查看二个矩形是否相交
if(findit(o[j][1],o[j][2],o[j][3],o[j][4],
up1,down1,left1,right1)
||findit(up1,down1,left1,right1,o[j][1],o[j][2],o[j][3],
o[j][4])){
//合并
if(o[j][1]>up1)
up1 = o[j][1];
if(o[j][2]<down1)
down1 = o[j][2];
if(o[j][3]<left1)
left1 = o[j][3];
if(o[j][4]>right1)
right1 = o[j][4];
o[j][0] = 0;
//*************************************************************************
j=0;//ＢＵＧ处，在将新矩形扩展后，应该从开始处再搜，有可能扩展也会把前面的交了．．
//*************************************************************************

}
}
}
o[i][0] = 1;
o[i][1] = up1;
o[i][2] = down1;
o[i][3] = left1;
}
int all = x*y;
for(i=1;i<=n;i++){
if(o[i][0] != 0){
all =all- (o[i][1]-o[i][2])*(o[i][4]-o[i][3]);
}
}
if(all<0)
all=0;
cout<<all<<endl;
}

return 0;
}

0 0