Zhuge Liang's Mines
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 44 Accepted Submission(s): 9
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
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
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.
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
4
先暴力找出所有的正方形(因为地雷最多只有20个,所以n^4完全没问题),再0,1背包下选那些正方形能选到最多正方形就行了。
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <math.h>
#include <queue>
#include <map> using namespace std;
typedef struct abcd
{
double x,y;
} abcd;
abcd a[];
int t;
int maxa;
int c[];
int b[][];
bool cmp(abcd x,abcd y)
{
if(x.x!=y.x)return x.x<y.x;
else return x.y<y.y;
}
void dfs(int x,int sum)
{
if(x>=t)
{
if(maxa<sum)
maxa=sum;
return ;
}
int i;
int fla=;
for(i=x;i<t;i++)
{
if((c[b[i][]]||c[b[i][]]||c[b[i][]]||c[b[i][]])==)
{
fla=;
c[b[i][]]=;c[b[i][]]=;c[b[i][]]=;c[b[i][]]=;
dfs(i+,sum+);
c[b[i][]]=;c[b[i][]]=;c[b[i][]]=;c[b[i][]]=;
}
}
if(!fla)
{
if(maxa<sum)
maxa=sum;
}
}
int fun(int i,int j,int x,int y)
{
if(a[i].x==a[j].x&&a[j].x==a[x].x) return ;
if(a[i].y==a[j].y&&a[j].y==a[x].y) return ;
if(a[i].x!=a[j].x||a[x].x!=a[y].x)
return ;
if(a[i].y!=a[x].y||a[j].y!=a[y].y)
return ;
if(a[j].y-a[i].y!=a[x].x-a[i].x)
return ;
else return ;
}
int main()
{
int n; while(scanf("%d",&n))
{
if(n==-)
break;
int i,j,x,y;
for(i=; i<n; i++)
{
scanf("%lf%lf",&a[i].x,&a[i].y);
}
sort(a,a+n,cmp);
memset(c,,sizeof(c));
maxa=;
t=;
for(i=; i<n; i++)
{
for(j=i+; j<n; j++)
{
for(x=j+; x<n; x++)
{
for(y=x+; y<n; y++)
{
if(fun(i,j,x,y))
{
b[t][]=i;
b[t][]=j;
b[t][]=x;
b[t][]=y;
t++;
}
}
}
}
}
dfs(,);
cout<<maxa<<endl;
}
}