UV 10336 rank the ages

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.