首页 > 代码库 > hdu 1198(并查集 )
hdu 1198(并查集 )
自从懂了并查集只后,感觉好多题都是并查集,就像哪一天的字典树一样,这道题一看就是一个并查集,最后查询父节点有几个,
难点:建模的时候应该吧上下联通的和左右联通的标记一下,只要他们和上下左右的都能连通,就把他们并到一个集合里面,我是只判断下和右即可,
源代码:
#include<stdio.h> #include<string.h> int up[8], down[8], right[8], left[8]; int par[2504]; char map[54][54]; char mp1[100][100]; char mp2[100][100]; void init() { up[0] = 'A'; up[1] = 'B'; up[2] = 'E'; up[3] = 'G'; up[4] = 'H'; up[5] = 'J'; up[6] = 'K'; down[0] = 'C'; down[1] = 'D'; down[2] = 'E'; down[3] = 'H'; down[4] = 'I'; down[5] = 'J'; down[6] = 'K'; left[0] = 'A'; left[1] = 'C'; left[2] = 'F'; left[3] = 'G'; left[4] = 'H'; left[5] = 'I'; left[6] = 'K'; right[0] = 'B'; right[1] = 'D'; right[2] = 'F'; right[3] = 'G'; right[4] = 'I'; right[5] = 'J'; right[6] = 'K'; memset(mp1, 0, sizeof(mp1)); memset(mp2, 0, sizeof(mp2)); for (int i = 0; i <= 6; i++){ for (int j = 0; j <= 6; j++){ mp1[down[i]][up[j]] = 1; mp2[right[i]][left[j]] = 1; } } } int findset(int x){ if(x!=par[x]) par[x]=findset(par[x]); return par[x]; } void unite(int x,int y){ int px=findset(x); int py=findset(y); if(px!=py) par[py]=px; } int main() { int M, N; init(); while (scanf("%d%d", &M, &N) != EOF){ if (M == -1 && N == -1) break; for (int i = 0; i < M; i++){ scanf("%s", map[i]); } for (int i = 0; i <= M * N; i++){ par[i] = i; } for (int i = 0; i < M; i++){ for (int j = 0; j < N; j++){ if (j < N - 1 && mp2[map[i][j]][map[i][j+1]] == 1){ int t1 = i * N + j; int t2 = i * N + j + 1; unite(t1, t2); } if (i < M - 1 && mp1[map[i][j]][map[i + 1][j]] == 1){ int t1 = i * N + j; int t2 = (i + 1) * N + j; unite(t1, t2); } } } int ans = 0; for (int i = 0; i < M * N; i++){ if (par[i] == i){ ans++; } } printf("%d\n", ans); } return 0; }
hdu 1198(并查集 )
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。