一些点,每个点不能重复选,问最多能选出几个正方形(边平行X,Y轴的)
思路:状压DP,先预处理出所有正方形,然后简单的状压DP即可
代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int n, have[7777], hn;
struct Point {
int x, y;
void read() {
scanf("%d%d", &x, &y);
}
} p[25], pp[4];
bool cmp(Point a, Point b) {
if (a.x == b.x) return a.y < b.y;
return a.x < b.x;
}
// 1 3
// 0 2
bool judge(int i, int j, int k, int l) {
int pn = 0;
pp[pn++] = p[i];
pp[pn++] = p[j];
pp[pn++] = p[k];
pp[pn++] = p[l];
sort(pp, pp + pn, cmp);
if (pp[0].x != pp[1].x) return false;
if (pp[0].y != pp[2].y) return false;
if (pp[1].y != pp[3].y) return false;
if (pp[2].x != pp[3].x) return false;
int len = pp[1].y - pp[0].y;
if (pp[3].x - pp[1].x != len) return false;
if (pp[3].y - pp[2].y != len) return false;
if (pp[2].x - pp[0].x != len) return false;
return true;
}
int dp[1<<20];
int dfs(int u) {
if (dp[u] != -1) return dp[u];
dp[u] = 0;
for (int i = 0; i < hn; i++) {
if ((u&have[i]) == have[i])
dp[u] = max(dp[u], dfs(u^have[i]) + 1);
}
return dp[u];
}
int main() {
while (~scanf("%d", &n) && n != -1) {
hn = 0;
for (int i = 0; i < n; i++) p[i].read();
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 (judge(i, j, k, l)) have[hn++] = ((1<<i)^(1<<j)^(1<<k)^(1<<l));
}
}
}
}
memset(dp, -1, sizeof(dp));
printf("%d\n", dfs((1<<n) - 1) * 4);
}
return 0;
}