hdu4739Zhuge Liang's Mines 暴搜dfs

//给出n个位置,问最多能找到几个正方形
//直接排序然后暴搜就行
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std ;
const int maxn = 30 ;
struct node
{
    int x , y ;
    bool operator < (struct node tmp)const
    {
        if(x == tmp.x)
        return y < tmp.y ;
        return x < tmp.x ;
    }
    bool operator == (struct node tmp)const
    {
        if(x == tmp.x && y == tmp.y)
        return true ;
        return false ;
    }
}a[maxn] ;
int b[maxn] ;
int vis[maxn] ;
int n ;
int judge()
{
    if(a[b[0]]==a[b[1]]||a[b[0]]==a[b[2]]||a[b[0]]==a[b[3]]||a[b[1]]==a[b[2]]||a[b[1]]==a[b[3]]||a[b[2]]==a[b[3]])
    return false ;
    if(a[b[0]].x!=a[b[1]].x||a[b[2]].x!=a[b[3]].x||a[b[0]].y!=a[b[2]].y||a[b[1]].y!=a[b[3]].y||(a[b[1]].y-a[b[0]].y != a[b[2]].x-a[b[0]].x))
    return false ;
    return true ;
}
int dfs(int step  , int sum , int len)
{
    if(len == 4)
    {
        if(judge())
        return dfs(0 , sum+1 , 0) ;
        else return sum ;
    }
    int ans = sum;
    for(int i = step+1;i <= n;i++)
    {
        if(vis[i])continue ;
        vis[i] = 1 ;
        b[len] = i ;
        ans =  max(ans , dfs(i , sum , len+1)) ;
        vis[i] = 0 ;
    }
    return ans ;
}
int main()
{
   // freopen("in.txt" , "r" , stdin) ;
    while(scanf("%d" , &n) && n!=-1)
    {
        for(int i = 1;i <= n;i++)
        scanf("%d%d" , &a[i].x , &a[i].y) ;
        sort(a+1 , a+1+n) ;
        memset(vis , 0 , sizeof(vis)) ;
        int ans = dfs(0 , 0 , 0) ;
        cout<<ans*4<<endl;
    }
    return  0 ;
}

你可能感兴趣的:(DFS)