Query set
First use an array a to save the A-K square on the left and right whether there is a river.
Then, use and check the set to determine the number of different sets.
# Include <cstdio> # include <iostream> # include <cstring> using namespace STD; # define N 55 # define M 55int A [11] [4] =,, 1, 1}; char map [N] [m]; int father [N * m]; int num [N * m]; int getfather (int x) {While (father [x]! =-1) {x = Father [X];} return X;} void Union (int x, int y) {int FX = getfather (X ); int FY = getfather (y); If (FX! = FY) Father [FX] = FY;} int main () {int n, m; while (~ Scanf ("% d", & N, & M) & N + M> = 0) {memset (father,-1, sizeof (father )); for (INT I = 0; I <n; I ++) CIN> map [I]; for (INT I = 0; I <n; I ++) for (Int J = 0; j <m; j ++) {if (I> = 1 & A [map [I] [J]-'a'] [0] & A [map [I-1] [J]-'a '] [2]) // shangunion (M * I + J, M * (I-1) + J ); if (I <n-1 & A [map [I] [J]-'a'] [2] & A [map [I + 1] [J]-'a '] [0]) // xiaunion (M * I + J, M * (I + 1) + J ); if (j> = 1 & A [map [I] [J]-'a'] [3] & A [map [I] [J-1]-'A'] [1]) // zuounion (M * I + J, M * I + J-1 ); if (j <M-1 & A [map [I] [J]-'a'] [1] & A [map [I] [J + 1]-'a '] [3]) // youunion (M * I + J, M * I + J + 1);} memset (Num, 0, sizeof (Num); For (INT I = 0; I <n * m; I ++) {If (father [I] =-1) num [I] = 1 ;} int ans = 0; for (INT I = 0; I <n * m; I ++) if (Num [I]) ans ++; printf ("% d \ n ", ans);} return 0 ;}