您的位置:首页 > 其它

hdu 4739 Zhuge Liang's Mines

2013-09-17 17:25 211 查看
当时没做出来,事后看了别人的博客才知道

#include <iostream>
#include <cstring>
#include <algorithm>
#include <stdio.h>
using namespace std;
const int maxx=110;
bool map[maxx][maxx];
int num[maxx][maxx];
int ans=0;
int n;
struct kiss
{
int xx,yy;
}test[22];
bool cmp(const kiss &a,const kiss &b)
{
if (a.yy!=b.yy)return a.yy<b.yy;
if(a.xx!=b.xx) return a.xx<b.xx;
}
void dfs(int pos,int val)
{
if (ans<val) ans=val;
if (pos>=n)return;

if (num[test[pos].xx][test[pos].yy]<=0) dfs(pos+1,val);
else
{
for (int i=pos+1;i<n; ++i)
{

if (test[i].yy!=test[pos].yy) break;
if (test[i].xx==test[pos].xx) continue;
int tmp=test[i].xx-test[pos].xx;
if (map[test[pos].xx][test[pos].yy+tmp] && map[test[i].xx][test[i].yy+tmp]
&& num[test[pos].xx][test[pos].yy+tmp]>0 && num[test[i].xx][test[i].yy+tmp]>0
&& num[test[pos].xx][test[pos].yy]>0 && num[test[i].xx][test[i].yy]>0)
{
num[test[pos].xx][test[pos].yy+tmp]--;
num[test[i].xx][test[i].yy+tmp]--;
num[test[pos].xx][test[pos].yy]--;
num[test[i].xx][test[i].yy]--;
dfs(pos+1,val+4);
num[test[pos].xx][test[pos].yy+tmp]++;
num[test[i].xx][test[i].yy+tmp]++;
num[test[pos].xx][test[pos].yy]++;
num[test[i].xx][test[i].yy]++;
}
}

dfs(pos+1,val);
}

}
int main()
{

int x,y;
while (scanf("%d",&n)!=EOF && n!=-1)
{
ans=0;
memset(map,0,sizeof(map));
memset(num,0,sizeof(num));
for (int i=0; i<n; ++i)
{
scanf("%d %d",&x,&y);
map[x][y]=1;
num[x][y]++;
test[i].xx=x;
test[i].yy=y;
}

sort(test,test+n,cmp);
dfs(0,0);
cout<<ans<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  dfs