HDU 1198 Farm Irrigation

Source: Internet
Author: User

Link:

Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1198

Question:

Problem descriptionbenny 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
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

ADC
Fjk
IHE

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.


Inputthere 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.


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


Sample Input

2 2DKHF3 3ADCFJKIHE-1 -1
 


Sample output

23

Question:

There is ~ K has a total of 9 types of fields, and the channels on each field are different (they connect to different outlets ). Then, these types of fields form a n * m-sized, larger field. Because the adjacent two fields may have interconnected channels (with interfaces ), therefore, only one water source is needed for the interconnected fields. The minimum number of water sources required.


Analysis:

BFS and DFS can be used, but it is more convenient to use and query sets. To use the query set, you only need to judge the four fields around a field. If there is an interface, combine the two fields into a tree and finally judge several trees.


Code:

// Check the set # include <cstdio> # include <cstring> # define n 55int N, M, F [N * n], rank [N * n]; int map [55] [55]; // left, top, right, bottom int dir [4] [2] = {0,-1}, {-1 }, {0, 1}, {1, 0}; // type of the field. In order, right, bottom, left, 1 at the exit, 0 int farm [11] [4] =, }, {0, 1, 1}, {1, 1, 0}, {1, 1, 1}; inline void initset () {for (INT I = 0; I <n * N; ++ I) f [I] = I, rank [I] = 0;} int find (int x) {int I, j = x; while (J! = F [J]) J = f [J]; while (X! = J) {I = f [X]; F [x] = J; X = I;} return J;} void Union (INT X, int y) {int A = find (x), B = find (y); if (a = B) return; If (rank [a]> rank [B]) f [B] = A; else {If (rank [a] = rank [B]) + + rank [B]; F [a] = B ;}} int main () {char ch; while (scanf ("% d % * C", & N, & M) & n> = 1 & M> = 1) {for (INT I = 0; I <n; ++ I) {for (Int J = 0; j <m; ++ J) {scanf ("% C", & Ch); map [I] [J] = CH-'A';} getchar ();} initset (); For (INT I = 0; I <n; ++ I) {for (Int J = 0; j <m; ++ J) {for (int K = 0; k <4; ++ K) {int dx = I + dir [k] [0], DY = J + dir [k] [1]; If (DX <0 | DX> = n | dy <0 | dy> = m) continue; if (k = 0) {// left if (farm [map [dx] [dy] [1] & farm [map [I] [J] [3]) {Union (dx * m + dy, I * m + J) ;}} else if (k = 1) {// upper if (farm [map [dx] [dy] [2] & farm [map [I] [J] [0]) {Union (dx * m + dy, I * m + J) ;}} else if (k = 2) {// right IF (farm [map [dx] [dy] [3] & farm [map [I] [J] [1]) {Union (dx * m + dy, I * m + J) ;}} else if (k = 3) {// lower if (farm [map [dx] [dy] [0] & farm [map [I] [J] [2]) {Union (dx * m + dy, I * m + J) ;}}} int CNT = 0; For (INT I = 0; I <n * m; ++ I) if (F [I] = I) ++ CNT; printf ("% d \ n", CNT);} return 0 ;}

-- The meaning of life is to give it meaning.

Original Http://blog.csdn.net/shuangde800 , By d_double (reprinted, please mark)

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.