https://www.acwing.com/problem/content/1404/
考察flood fill 和 图的hash。
这题有两问:1、找出图中的全部连通块
2、根据形状给给将相似的连通块换上同一个字符
对于第一问,可以直接应用flood fill算法。
对于第二问,我们可以通过hash的方式辨别不同的连通块,我们需要保证相似的连通块的hash值相同,不相似的连通块的hash值尽量不同。
通过观察可以发现,假设两个连通块相似,构成这个连通块的两两之间的距离的和是相等的,故可依此对连通块进行hash。
(在考试的时候,对于字符串和树图的hash一定不要去处理冲突,因为如果处理冲突,那么hash的意义也就不存在了,如果冲突就在写一个hash函数)
1 #include<iostream> 2 #include<cmath> 3 using namespace std; 4 const int N=110; 5 const double eps=1e-6; 6 typedef pair<int,int> PII; 7 int n,m; 8 char g[N][N]; 9 PII locate[N*N]; 10 int cnt=0; 11 double get_dist(PII a,PII b){//获得两点之间的距离 12 double x=a.first-b.first; 13 double y=a.second-b.second; 14 return sqrt(x*x+y*y); 15 } 16 double get_hash(){//获得当前连通块的hash值 17 double sum=0; 18 for(int i=0;i<cnt;i++){ 19 for(int j=i+1;j<cnt;j++){ 20 sum+=get_dist(locate[i],locate[j]); 21 } 22 } 23 return sum; 24 } 25 char get_id(double hash){//找到hash值对应的字母 26 static double mp[40]; 27 static int id=0; 28 for(int i=0;i<id;i++){ 29 if(fabs(hash-mp[i])<eps){ 30 return 'a'+i; 31 } 32 } 33 mp[id]=hash; 34 return 'a'+id++; 35 } 36 void dfs(int a,int b){//flood fill 37 int u[]={-1,-1,-1,0,1,1,1,0}; 38 int v[]={-1,0,1,1,1,0,-1,-1}; 39 locate[cnt++]={a,b}; 40 g[a][b]='0'; 41 for(int i=0;i<8;i++){ 42 int x=a+u[i],y=b+v[i]; 43 if(x>=0&&x<n&&y>=0&&y<m&&g[x][y]=='1'){ 44 dfs(x,y); 45 } 46 } 47 } 48 int main(void){ 49 cin>>m>>n; 50 for(int i=0;i<n;i++){ 51 cin>>g[i]; 52 } 53 for(int i=0;i<n;i++){ 54 for(int j=0;j<m;j++){ 55 if(g[i][j]=='1'){ 56 cnt=0; 57 dfs(i,j); 58 char c=get_id(get_hash()); 59 for(int k=0;k<cnt;k++){ 60 g[locate[k].first][locate[k].second]=c; 61 } 62 } 63 } 64 } 65 for(int i=0;i<n;i++) cout<<g[i]<<endl; 66 return 0; 67 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端