hdu 4739 Zhuge Liang's Mines

本文介绍了一种解决特定布局中寻找最多四个点能够形成“四连吻”形状的问题的方法。通过深度优先搜索(DFS)算法遍历所有可能的组合,并使用辅助数组记录每个位置上“吻”的数量和可能性,最终找出最大数量的四连吻布局。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

当时没做出来,事后看了别人的博客才知道


#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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值