连通块判重

连通块判重hash函数设计

连通块判重

统计连通块种类和数量,数量比较容易使用洪泛即可快速完成,主要是统计种类。因此本文重点放在连通块判重上。
判重一般可以联想到hash,难点也是hash函数的设计,如何避免冲突。

例题

T1 图像存储
只有上下左右连通,平移重合则相似
hash函数设计
把dfs第几步走的方向作为hash的参数,直接用string存储,步数做一个取模来防止string太大了
如果不添加是第几步的话会冲突
在这里插入图片描述

#include<bits/stdc++.h>
using namespace std;
int n,m,cnt;
const int N=1e3+10;
char g[N][N];
int xx[]={1,-1,0,0};
int yy[]={0,0,1,-1};

string dfshash,tmp;
set<string>s;
void dfs(int a,int b,int s)
{
    g[a][b]='0';
    for(int i=0;i<4;i++)
    {
        int dx=xx[i]+a;
        int dy=yy[i]+b;
        if(dx>=0&&dy>=0&&dx<n&&dy<m&&g[dx][dy]=='1')
        {
        	int step=s%157;
        	tmp.clear();
        	char c=i+'1';
        	tmp+=c;
        	while(step)
        	{
        		int t=step%=10;
        		step/=10;
        		tmp+=t+'0';
			}
            dfshash+=tmp;
            dfs(dx,dy,s+1);
        }
    }
}
int main()
{
    while(cin>>n>>m)
    {
        if(n==0&&m==0)break;
        cnt=0;
        s.clear();
        memset(g,0,sizeof g);
        for(int i=0;i<n;i++)cin>>g[i];
        
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                if(g[i][j]=='1')
                {
                    ++cnt;
                    dfshash.clear();
                    dfs(i,j,1);
           //         cout<<i<<" "<<j<<" "<<dfshash<<endl;
                    s.insert(dfshash);
                }
            }
        }
        
        cout<<cnt<<" "<<s.size()<<endl;
    }
    return 0;
}

大佬的写法
hash函数设计:
把步数换成了base累积,同时采用双hash降低冲突概率
推荐博客

#include <iostream>
#include <map>
#define mod 998244353
using namespace std;
int a[1005][1005],now1,now2;
const int fx[5]={0,0,1,-1},fy[5]={1,-1,0,0}; 
const int base1=27;
const int base2=23;
inline void dfs(int x,int y)
{
    for(int i=0;i<=3;i++)
    {
        int nx=x+fx[i],ny=y+fy[i];
        if(a[nx][ny])
        {
            now1=((long long)now1*base1+i+3)%mod;
            now2=((long long)now2*base2+i+7)%mod;
            a[nx][ny]=0,dfs(nx,ny);
        }
    }
    now1=((long long)now1*base1+11)%mod;
    now2=((long long)now2*base2+13)%mod;//双哈希操作序列
}
map <int,int> mp1,mp2;
int main(int argc, char** argv) {
    int n,m;
    while(cin >> n >> m)
    {
        mp1.clear(),mp2.clear();
        if(!n) break;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                char c;
                cin >> c;
                if(c=='1') a[i][j]=1;
                else a[i][j]=0;
            }
        }
        int cnt=0,ans=0;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                if(a[i][j])
                {
                    now1=now2=0;
                    a[i][j]=0,dfs(i,j);
                    ++cnt;
                    if(!mp1[now1]||!mp2[now2]) ++ans;
                    mp1[now1]=mp2[now2]=1;//map去重
                }
            }
        }
        cout << cnt << " " << ans << "\n"; 
    }
    return 0;
}

T2 星空之夜
八个方向,旋转翻转也算相似
hash函数设计:
把连通块内点存下来,以块与块内的欧式几何距离之和hash
注意要使用double否则极其容易冲突,同时需要注意浮点数的阈值设置

#include<bits/stdc++.h>
using namespace std;

#define x first
#define y second

typedef pair<int,int> PII;

const int N=110;
const double eps=1e-6;
int n,m;
char g[N][N];
PII q[N*N];
int top;

double get_dist(PII a,PII b)
{
	double dx=a.x-b.x;
	double dy=a.y-b.y;
	return sqrt(dx*dx+dy*dy);
}

double get_hash()
{
	double sum=0;
	for(int i=0;i<top;i++)
		for(int j=i+1;j<top;j++)
			sum+=get_dist(q[i],q[j]);
			
	return sum;
}

char get_id(double key)
{
	static double hash[30];
	static int id=0;
	for(int i=0;i<id;i++)
		if(fabs(hash[i]-key)<eps)
			return i+'a';
	hash[id++]=key;
	return id-1+'a';
}

void dfs(int a,int b)
{
	q[top++]={a,b};
	g[a][b]='0';
	for(int x=a-1;x<=a+1;x++)
		for(int y=b-1;y<=b+1;y++)
		{
			if(x==a&&y==b)continue;
			if(x>=0&&x<n&&y>=0&&y<m&&g[x][y]=='1')
				dfs(x,y);
		}
}
int main()
{
	cin>>m>>n;
	for(int i=0;i<n;i++)cin>>g[i];
	
	for(int i=0;i<n;i++)
		for(int j=0;j<m;j++)
		if(g[i][j]=='1')
		{
			top=0;
			dfs(i,j);
			char c=get_id(get_hash());
			for(int k=0;k<top;k++)
			{
				g[q[k].x][q[k].y]=c;	
			}
		}
	
	for(int i=0;i<n;i++)cout<<g[i]<<endl;
}

hash函数小结

字符串存储:存储方便但要注意不要让字符串太大
数值存储:取模的质数稍微开大些,如果是浮点数注意阈值设计

冲突处理:双hash,多次hash,添加参数

图的hash一般考虑:距离,dfs序

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值