hdu 1241
来源:互联网 发布:手机淘宝怎么做 编辑:程序博客网 时间:2024/06/03 22:03
主题思想: 这题核心是Union Set ,但是需要自己以搜索的方式,构建哪些边是联通的信息, 搜索的时候,本来需要8个方向都要判断,但是由于从上往下,从左往右搜索,因此只需要对4个方向进行判断,避免了重复,
AC代码:
#include <iostream>#include<cstdio>#include<cstring>#include<string>using namespace std;const int maxn=105;char g[maxn][maxn];int a[maxn*maxn];int h[maxn*maxn];int dir[4][2]={1,0,0,1,1,1,1,-1}; //d,r,rd,ldint Find(int p){ if(a[p]==p) return p; a[p]=Find(a[p]); return a[p];}void UN(int p,int q){ p=Find(p); q=Find(q); if(p!=q){ if(h[p]<h[q]){ a[q]=a[p]; h[p]+=h[q]; }else{ a[p]=a[q]; h[q]+=h[p]; } }}int main(){ int n,m; string s; int xx,yy; while(scanf("%d%d",&m,&n)!=EOF){ if(m==0) break; memset(a,-1,sizeof(a)); for(int i=0;i<m;i++){ cin>>s; for(int j=0;j<n;j++){ g[i][j]=s[j]; if(g[i][j]=='@'){ a[i*n+j]=i*n+j; } } } //init for(int i=0;i<m*n;i++){ h[i]=1; } //handle for(int i=0;i<m;i++){ for(int j=0;j<n;j++){ if(g[i][j]=='@'){ for(int k=0;k<4;k++){ xx=i+dir[k][0]; yy=j+dir[k][1]; if(xx<0||xx>=m||yy<0||yy>=n||g[xx][yy]=='*')continue; if(g[xx][yy]=='@') UN(i*n+j,xx*n+yy); } } } } //get the ans int ans=0; for(int i=0;i<m*n;i++){ if(a[i]==i)ans++; } printf("%d\n",ans); } return 0;}
阅读全文
0 0
- hdu 1241
- HDU 1241
- hdu 1241
- hdu 1241
- hdu 1241
- hdu 1241
- hdu-1241
- hdu 1241
- hdu 1241
- HDU 1241
- hdu 1241
- hdu-1241
- hdu 1241
- HDU(1241)
- hdu 1241
- Hdu 1241
- hdu 1241
- HDU 1241
- 【九度OJ】1010:A + B
- java中弹出对话框的应用
- 鼠标经过展开图片
- hibernate常见错误
- 数据库中间件2:关于连接异构数据库的性能问题
- hdu 1241
- leetcode 672. Bulb Switcher II
- 触宝科技2018校招笔试题
- Python学习-基础语法
- 【点的定位】Save the Students! UVALive
- 利用内存文件(共享内存)实现简单的数据库索引
- 守护进程
- Failed to post notification on channel "null"消息栏无法显示
- sql语句与执行update更新操作内的参数顺序关系