hdu 4739 Zhuge Liang's Mines 随机化

Zhuge Liang's Mines

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=4739

Description

In the ancient three kingdom period, Zhuge Liang was the most famous and smartest military leader. His enemy was Shima Yi, who always looked stupid when fighting against Zhuge Liang. But it was Shima Yi who laughed to the end.

Once, Zhuge Liang sent the arrogant Ma Shu to defend Jie Ting, a very important fortress. Because Ma Shu is the son of Zhuge Liang's good friend Ma liang, even Liu Bei, the Ex. king, had warned Zhuge Liang that Ma Shu was always bragging and couldn't be used, Zhuge Liang wouldn't listen. Shima Yi defeated Ma Shu and took Jie Ting. Zhuge Liang had to kill Ma Shu and retreated. To avoid Shima Yi's chasing, Zhuge Liang put some mines on the only road. Zhuge Liang deployed the mines in a Bagua pattern which made the mines very hard to remove. If you try to remove a single mine, no matter what you do ,it will explode. Ma Shu's son betrayed Zhuge Liang , he found Shima Yi, and told Shima Yi the only way to remove the mines: If you remove four mines which form the four vertexes of a square at the same time, the removal will be success. In fact, Shima Yi was not stupid. He removed as many mines as possible. Can you figure out how many mines he removed at that time?

The mine field can be considered as a the Cartesian coordinate system. Every mine had its coordinates. To simplify the problem, please only consider the squares which are parallel to the coordinate axes.

Input

There are no more than 15 test cases.
In each test case:

The first line is an integer N, meaning that there are N mines( 0 < N <= 20 ).

Next N lines describes the coordinates of N mines. Each line contains two integers X and Y, meaning that there is a mine at position (X,Y). ( 0 <= X,Y <= 100)

The input ends with N = -1.

Output

For each test case ,print the maximum number of mines Shima Yi removed in a line.

Sample Input

3
1 1
0 0
2 2
8
0 0
1 0
2 0
0 1
1 1
2 1
10 1
10 0
-1

Sample Output

0
4

HINT

题意

每次可以去掉构成正方形的四个点,问你最多去掉多少个点

题解:

我不知道正解是爆搜还是状压……

反正我是随机化乱搞的……

代码

#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define test freopen("test.txt","r",stdin)
const int maxn=2501;
#define mod 1000000009
#define eps 1e-9
const int inf=0x3f3f3f3f;
const ll infll = 0x3f3f3f3f3f3f3f3fLL;
inline ll read()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
//**************************************************************************************

struct node
{
    int x,y;
};
struct squal
{
    int kiss[4];
};
bool cmp(node a,node b)
{
    if(a.x==b.x)
        return a.y<b.y;
    return a.x<b.x;
}
node a[40];
int dp[40];
squal dis[300];
int n;
int dd;
void pre()
{
    for(int i=0;i<n;i++)
    {
        for(int j=i+1;j<n;j++)
        {
            for(int k=j+1;k<n;k++)
            {
                for(int t=k+1;t<n;t++)
                {
                    if(a[i].x==a[j].x&&a[i].y==a[k].y&&a[j].y==a[t].y&&a[k].x==a[t].x&&a[j].y-a[i].y==a[k].x-a[i].x)
                    {
                        dis[dd].kiss[0]=i;
                        dis[dd].kiss[1]=j;
                        dis[dd].kiss[2]=k;
                        dis[dd++].kiss[3]=t;
                    }
                }
            }
        }
    }
}
int main()
{

    while(cin>>n)
    {
        if(n==-1)
            break;
        memset(dis,0,sizeof(dis));
        memset(a,0,sizeof(a));
        memset(dp,0,sizeof(dp));
        for(int i=0;i<n;i++)
           a[i].x=read(),a[i].y=read();
        sort(a,a+n,cmp);
        dd=0;
        pre();
        if(dd==0)
        {
            puts("0");
            continue;
        }
        int flag[30];
        int ans=0;
        for(int i=0;i<1000;i++)
        {
            int ans1=0;
            memset(flag,0,sizeof(flag));
            for(int j=0;j<1000;j++)
            {
                int flag2=1;
                int aaa=rand()%dd;
                for(int k=0;k<4;k++)
                {
                    if(flag[dis[aaa].kiss[k]])
                        flag2=0;
                }
                if(flag2)
                {
                    ans1+=4;
                    for(int k=0;k<4;k++)
                    {
                        flag[dis[aaa].kiss[k]]++;
                    }
                }
            }
            ans=max(ans,ans1);
        }
        cout<<ans<<endl;
    }
}
原文地址:https://www.cnblogs.com/qscqesze/p/4659939.html