HDU 1198-farm Irrigation

Source: Internet
Author: User

Problem Descriptionbenny have a spacious farm land to irrigate. The farm land was a rectangle, and is divided to a lot of SAMLL squares. Water pipes is placed in these squares. Different Square has a Different type of pipe. There is one 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 in the over the pipes farm. For example, if he has a map

Adc
Fjk
IHE

Then the water pipes is distributed like


Figure 2

Several Wellsprings is 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 would have a good harvest in autumn.

Now Benny wants to know on least how many wellsprings should is found to has the whole farm land irrigated. Can you help him?

Note:in The above example, at least 3 wellsprings is needed, as those red points in Figure 2 show. Inputthere is several test cases! The first line contains is 2 integers m and N, then M lines follow. In each of these lines, there is N characters, in the range of ' A ' to ' K ', denoting the type of water pipe over the Corre Sponding 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 Input2 2DKHF3 3adcfjkihe-1-1 sample Output23 Roughly test instructions: such as 11 kinds of land blocks, blocks of green lines for the land block repaired canals, now a piece of land from the above various land blocks, need watering, ask how many wells to hit. Problem-solving ideas: And check set, find the number of different sets, is the number of root nodes.
1#include <cstdio>2 using namespacestd;3 intPhp: One][4]={{1,1,0,0},{0,1,1,0},{1,0,0,1},{0,0,1,1},4{0,1,0,1},{1,0,1,0},{1,1,1,0},{1,1,0,1},5{1,0,1,1},{0,1,1,1},{1,1,1,1}};6 intf[ the];Charmap[ -][ -];7 intsfintx) {returnx==f[x]?x:f[x]=SF (f[x]);};8 intMain () {9     intM,n;Ten      while(SCANF ("%d%d", &m,&n),m+n>0){ One          for(intI=0; i<m*n;i++) f[i]=i; A          for(intI=0; i<m;i++) scanf ("%s",&map[i]); -          for(intI=0; i<m;i++)//Start processing -              for(intj=0; j<n;j++){ the                 intx=map[i][j]-'A'; -                 if(i+1<L) { -                    intd=map[i+1][j]-'A'; -                    if(pip[x][3]&&pip[d][1]){//and right-connected +                         intFX=SF (i*n+j); -                         intFA=SF ((i+1) *n+j); +f[fx]=FA; A                     }  at                 } -                 if(j+1<N) { -                     intr=map[i][j+1]-'A'; -                     if(pip[x][2]&&pip[r][0]){//and the lower connection -                         intFX=SF (i*n+j); -                         intFA=SF (i*n+j+1); inf[fx]=FA; -                     }  to                 } +             }  -         intans=0; the          for(intI=0; i<m*n;i++)if(f[i]==i) ans++; *printf"%d\n", ans); $}return 0;Panax Notoginseng}

HDU 1198-farm Irrigation

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.