Zoj 2412 Farm Irrigation

Source: Internet
Author: User
Farm Irrigation Time Limit: 2 seconds memory limit: 65536 KB

Benny has a spacious farm land to irrigate. the farm land is a rectangle, and is divided into a lot of Samll squares. water pipes are placed in these squares. different square has a different type of pipe. there are 11 types of pipes, which is marked from A to K, as Figure 1 shows.


Figure 1

Benny has a map of his farm, which is an array of marks denoting the distribution of water pipes over the whole farm. For example, if he has a map

ADCFJKIHE

Then the water pipes are distributed like


Figure 2

Several wellsprings are found in the center of some squares, so water can flow along the pipes from one square to another. if water flow crosses one square, the whole farm land in this square is irrigated and will have a good harvest in autumn.

Now Benny wants to know at least how could wellsprings shocould be found to have the whole farm land irrigated. Can you help him?

Note: In the above example, at least 3 wellsprings are needed, as those red points in Figure 2 show.

Input

There are several test cases! In each test case, the first line contains 2 integers m and n, then M lines follow. in each of these lines, there are n characters, in the range of 'A' to 'k', denoting the type of water pipe over the corresponding square. A negative M or N denotes the end of input, else you can assume 1 <= m, n <= 50.

Output

For each test case, output in one line the least number of wellsprings needed.

Sample Input

2 2DKHF3 3ADCFJKIHE-1 -1

Sample output

23

Problem-solving code: Use and query Sets

1 # include <stdio. h> 2 # include <string. h> 3 # include <iostream> 4 using namespace STD; 5 6 const int max_n = 2520; 7 int door [max_n] [4]; 8 int Fa [max_n]; 9 int root [max_n]; 10 char map [51] [51]; 11 12 INT find (int x) {13 return x = Fa [x]? X: Fa [x] = find (Fa [x]); 14} 15 16 void Union (int x, int y) {17 int xx = find (X ); 18 int YY = find (y); 19 if (XX! = YY) {20 Fa [XX] = YY; 21} 22} 23 24 int main () {25 int n, m; 26 int I, j, ANS, T; 27 int A, B; 28 while (~ Scanf ("% d", & N, & M) & n> 0 & M> 0) {29 for (I = 0; I <N; I ++) 30 scanf ("% s", map [I]); 31 T = 0; 32 memset (door, 0, sizeof (door )); 33 memset (root, 0, sizeof (Root); 34 for (I = 0; I <n; I ++) {// convert a two-dimensional array to a one-dimensional array 35 for (j = 0; j <m; j ++) {36 switch (Map [I] [J]) {37 Case 'A': Door [T] [0] = door [T] [1] = 1; break; 38 case 'B ': door [T] [1] = door [T] [2] = 1; break; 39 case 'C ': door [T] [3] = door [T] [0] = 1; break; 40 case 'D ': door [T] [2] = door [T] [3] = 1; break; 41 case 'E ': door [T] [1] = door [T] [3] = 1; break; 42 case 'F ': door [T] [0] = door [T] [2] = 1; break; 43 case 'G ': door [T] [0] = door [T] [1] = door [T] [2] = 1; break; 44 case 'H ': door [T] [3] = door [T] [0] = door [T] [1] = 1; break; 45 case 'I ': door [T] [2] = door [T] [3] = door [T] [0] = 1; break; 46 case 'J ': door [T] [1] = door [T] [2] = door [T] [3] = 1; break; 47 case 'K ': door [T] [0] = door [T] [1] = door [T] [2] = door [T] [3] = 1; break; 48} 49 Fa [T] = T; 50 t ++; 51} 52} 53 for (I = 0; I <n-1; I ++) {54 for (j = 0; j <m-1; j ++) {55 A = I * m + J; 56 B = a + 1; 57 if (Door [a] [2] & door [B] [0]) // It is equivalent to 58 Union (A, B) for the horizontal correspondence of two-dimensional arrays ); // combine the used and queried sets belonging to the same MPs queue group 59 B = a + m; 60 if (Door [a] [3] & door [B] [1]) // equivalent to 61 Union (a, B) corresponding to the vertical direction of a two-dimensional array ); 62} 63 A = I * m + J; // two-dimensional boundary 64 B = a + m; 65 if (Door [a] [3] & door [B] [1]) 66 Union (a, B); 67} 68 for (j = 0; j <m-1; j ++) {// two-dimensional boundary 69 A = I * m + J; 70 B = a + 1; 71 If (Door [a] [2] & door [B] [0]) 72 Union (a, B); 73} 74 ans = 0; 75 for (I = 0; I <t; I ++) 76 root [find (I)] = 1; // find a total of 77 for (I = 0; I <t; I ++) 78 If (root [I]) 79 ans ++; // statistical result 80 printf ("% d \ n", ANS); 81} 82 return 0; 83}
View code

 

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.