您的位置:首页 > 其它

hdu4739 状态压缩dp

2015-04-14 09:53 288 查看
http://acm.hdu.edu.cn/showproblem.php?pid=4739

Problem 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

/**
hdu4739 状态压缩DP
题目大意:在平面直角坐标系中给出n个点,在每个点只用一次的情况下,问用多少个点,可以围成最多的三角形
解题思路:由于题目只有20个点,我们用一个二进制位来压缩状态,然后进行状态转移即可。
转移方程: dp[s]=max(dp[s^t]+4,dp[s]);,其中t中已经取了四个二进制位,并且这四个位可以构成一个题目要求的正方形
值得一提的是:题目说只考虑与各边与坐标轴平行的正方形,但是并不排除不存在与坐标轴不平行的正方形
*/
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <vector>
#include <math.h>
using namespace std;
const int maxn=21;
vector <int> vec[maxn];
struct node
{
int x,y;
bool operator <( const node &other)const
{
if(y==other.y)
return x<other.x;
return y<other.y;
}
} a[maxn];

int dp[(1<<20)+7],n;

bool ok(int i,int j,int k,int l)///判断是否是正方形时应该注意,只能算个边与坐标轴平行的正方形
{
/**排过序之后对应各点的位置
k------l
|      |
|      |
|      |
i------j
*/
if(a[i].y!=a[j].y||a[i].x!=a[k].x||a[j].x!=a[l].x||a[k].y!=a[l].y)///四个角是直角且边平行月坐标轴
return false;
if(abs(a[i].x-a[j].x)!=abs(a[i].y-a[k].y))///相邻的两边相等
return false;
return true;
}

int main()
{
while(~scanf("%d",&n))
{
if(n==-1)break;
for(int i=0; i<n; i++)
{
scanf("%d%d",&a[i].x,&a[i].y);
}
sort(a,a+n);
for(int i=0; i<n; i++)vec[i].clear();
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 l=k+1; l<n; l++)
{
if(ok(i,j,k,l))
{
int s=0;
s|=(1<<i);
s|=(1<<j);
s|=(1<<k);
s|=(1<<l);
vec[i].push_back(s);
}
}
}
}
}
memset(dp,0,sizeof(dp));
for(int s=0; s<(1<<n); s++)
{
for(int i=0; i<n; i++)
{
if(s&(1<<i))
{
for(int j=0; j<vec[i].size(); j++)
{
int t=vec[i][j];
if((s|t)==s)
{
dp[s]=max(dp[s^t]+4,dp[s]);
}
}
}
}
}
printf("%d\n",dp[(1<<n)-1]);
}
return 0;
}
/**

10
0 1
0 2
1 0
1 1
1 2
1 3
2 0
2 1
2 2
2 3

answer:8

*/


Problem 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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: