wustoj1280What’ s Soapbear(简单计算几何)

1280: What’ s Soapbear

Time Limit: 2 Sec   Memory Limit: 128 MB
[ Submit][ Status][ Web Board]

Problem Description

Soapbear is a bear who likes collecting soaps, of course not picking up soaps. Soapbear has collected many soaps of different shapes. To simplify the problem, you can assume they are all polygons without self-intersect. Besides, since Soapbear has special interest in anything with axial symmetry, all of his soap are of axial symmetry. Recently Wallace picked up some soaps and he wonders which of them may belong to Soapbear.

Input

There are multiple test cases in the input file. There first line of the input is an integer T indicates the number of the test cases. Each test case starts with a line containing a integer N (3 <= N <= 12), the number of vertexes of the soap. Then N lines follow, each of which contains two integers Xi and Yi, indicate the coordinate of the i-th vertex in Cartesian coordinate system. All the integers given are no more than 10000 in absolute value. All the coordinates are given in clockwise order.

Output

For each test case, if the polygon is of axial symmetry, print a line “The soap may belong to the bear !”, otherwise print a line “The soap does not belong to the bear !” (without quotation marks).

Sample Input

2
12
0 1
0 3
1 3
1 4
2 4
2 5
4 5
4 3
3 3
3 2
2 2
2 1
4
4 0
4 2
6 2
7 0

Sample Output

The soap may belong to the bear !
The soap does not belong to the bear !

HINT

Source

Difficulty



    题目大意:给你一个多边形,让你判断这个多边形是不是轴对称图形。

解题思路:由于最多只有十二个点,可以直接暴力枚举。如果轴对称,肯定有个对称轴,我们枚举任意两个点的中垂线来作为对称轴。然后处理其它的点,如果点在中垂线上,直接不考虑这点,其它的点都需要配对,这样才可以达到轴对称。详见代码:

题目地址:http://wusttest.sinaapp.com/problem.php?id=1280

AC代码:
#include<iostream>
#include<cmath>
#include<cstdio>
#include<sstream>
#include<cstdlib>
#include<string>
#include<string.h>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<ctime>
#include<bitset>
#define eps 1e-6
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define ll __int64
#define LL long long
#define lson l,m,(rt<<1)
#define rson m+1,r,(rt<<1)|1
#define M 1000000007
using namespace std;
 
#define Maxn 15
struct nod
{
    double x;
    double y;
}node[Maxn];
 
int visi[Maxn];
 
int main()
{
    int t,n,i,j,k,l,p;
    cin>>t;
 
    while(t--)
    {
        cin>>n;
        for(i=0;i<n;i++)
            cin>>node[i].x>>node[i].y;
 
        double x,y;
        int flag=0;
        nod p1,p2,p3;
 
        for(i=0;i<n&&!flag;i++)
        {
            for(j=i+1;j<n&&!flag;j++)
            {
                memset(visi,0,sizeof(visi));
                visi[i]=1;
                visi[j]=1;
 
 
                x=(node[i].x+node[j].x)/2;
                y=(node[i].y+node[j].y)/2;
 
                p1.x=node[j].x-node[i].x;
                p1.y=node[j].y-node[i].y;
 
 
                for(k=0;k<n;k++)
                {
                    if(visi[k])
                        continue;
 
                    p2.x=node[k].x-x;
                    p2.y=node[k].y-y;
                    if(fabs(p1.x*p2.x+p1.y*p2.y)<eps)
                    {
                        visi[k]=1;
                        continue;
                    }
                }
 
                for(k=0;k<n;k++)
                {
                    if(visi[k])
                        continue;
 
                    for(p=0;p<n;p++)
                    {
                        if(p==k||visi[p])
                            continue;
 
                        double x1,y1;
                        x1=(node[k].x+node[p].x)/2;
                        y1=(node[k].y+node[p].y)/2;
                        p2.x=x1-x;
                        p2.y=y1-y;
                        p3.x=node[k].x-node[p].x;
                        p3.y=node[k].y-node[p].y;
 
 
                        if(fabs(x1-x)<eps&&fabs(y1-y)<eps)
                            continue;
 
                        if(fabs(p1.x*p2.x+p1.y*p2.y)<eps&&fabs(p2.x*p3.x+p2.y*p3.y)<eps)
                        {
                            visi[k]=1;
                            visi[p]=1;
                            flag=1;
                            break;
                        }
                    }
                }
 
                int tt=0;
                for(l=0;l<n;l++)
                    if(!visi[l])
                        tt=1;
 
                if(tt==0) flag=1;
            }
        }
 
        if(flag) puts("The soap may belong to the bear !");
        else puts("The soap does not belong to the bear !");
    }
    return 0;
}
 
/*
23
6
4 2
4 0
4 -2
6 4
6 2
6 0
 
3
0 0
2 0
1 2
*/


你可能感兴趣的:(计算几何)