Hdu 1198 (query set), hdu1198
After understanding and querying only sets, I feel that many questions are all combined, just like the dictionary tree on which day, this question is a single query set, finally, several parent nodes are queried,
Difficulty: During modeling, you should mark up and down China Unicom and left and right Unicom. As long as they can be connected to up and down China, they will be included in a set, I can only judge the bottom and right,
Source code:
#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;}