题意:
给出宝藏个数,然后给出每个宝藏的位置;
如果四个宝藏构成正方形就能取走;
问最多取走几个;
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 25;
int n, ans;
int cnt[105][105];
struct node {
int x;
int y;
bool operator<(node a) const {
if(x != a.x) {
return x < a.x;
}
else
return y < a.y;
}
}p[N];
void dfs(int c, int s) {
if(s > ans)
ans = s;
if(c >= n)
return ;
int cx = p[c].x;
int cy = p[c].y;
for(int i = c + 1; i < n; i++) {
int px = p[i].x;
int py = p[i].y;
if(px > cx)
break;
if(py == cy)
continue;
int l = py - cy;
if(cnt[cx][cy] && cnt[cx][cy + l] && cnt[cx + l][cy] && cnt[cx + l][cy + l]) {
cnt[cx][cy]--;
cnt[cx][cy + l]--;
cnt[cx + l][cy]--;
cnt[cx + l][cy + l]--;
dfs(c + 1, s + 4);
cnt[cx][cy]++;
cnt[cx][cy + l]++;
cnt[cx + l][cy]++;
cnt[cx + l][cy + l]++;
}
}
dfs(c + 1, s);
}
int main() {
while(scanf("%d",&n) && n != -1) {
memset(cnt, 0, sizeof(cnt));
for(int i = 0; i < n; i++) {
scanf("%d%d",&p[i].x, &p[i].y);
cnt[p[i].x][p[i].y]++;
}
sort(p, p + n);
ans = 0;
dfs(0, 0);
printf("%d\n",ans);
}
}思路:
回溯,看看当前点能不能作为正方形上角,然后枚举右上角,得出正形的边长l;
看看(x,y) , (x + l, y) (x, y + l) (x + l , y + l)是不是都有宝藏:
原文:http://blog.csdn.net/yeyeyeguoguo/article/details/44926359