1 # include <iostream> 2 # include <cstring> 3 using namespace STD; 4 void DFS (int x, int y ); 5 char mapstring [1000] [1000] = {0}; // The "map" 6 bool vis [1000] [1000] = {0} to be judged }; // whether the node has been accessed, 7 int main () 8 {9 int H, W; // H: height; W: width10 int line; 11 int maxchar; // Number of countries used by the language; 12 13 while (CIN> line) {14 for (INT m = 0; m <line; m ++) 15 {16 memset (VIS, 0, sizeof (VIS); 17 int letter [29] = {0}; // "language" Statistics 18 maxchar = 0; 19 // The above should be Initialize 20 CIN> H> W; 21 in this loop // enter 22 for (INT I = 1; I <= H; I ++) {23 for (Int J = 1; j <= W; j ++) {24 CIN> mapstring [I] [J]; 25} 26} 27 for (Int J = 1; j <= H; j ++) {28 for (int K = 1; k <= W; k ++) {29 If (vis [J] [k]! = 1) {30 maxchar = max (++ letter [mapstring [J] [k]-'a'], maxchar); 31 DFS (j, k ); 32} 33} 34} 35 cout <"world #" <m + 1 <Endl; 36 for (Int J = maxchar; j> = 1; j --) {37 for (int K = 0; k <26; k ++) {38 If (letter [k] = J) {39 cout <(char) ('A' + k) <":" <j <Endl; 40} 41} 42 43} 44} 45} 46 Return 0; 47} 48 void DFS (int x, int y) 49 {50 vis [x] [Y] = 1; 51 if (mapstring [x + 1] [Y] = mapstring [x] [Y]) & (vis [x + 1] [Y]! = 1) DFS (x + 1, Y); 52 If (mapstring [x-1] [Y] = mapstring [x] [Y]) & (vis [x-1] [Y]! = 1) DFS (x-1, Y); 53 If (mapstring [x] [Y + 1] = mapstring [x] [Y]) & (vis [x] [Y + 1]! = 1) DFS (X, Y + 1); 54 if (mapstring [x] [Y-1] = mapstring [x] [Y]) & (vis [x] [Y-1]! = 1) DFS (x, Y-1); 55}
Algorithm: DFS;
Idea: recursion.
UV 10336 rank the ages