HDU 1198 Farm Irrigation (and collection)

Source: Internet
Author: User

Test instructions

Benny has 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.

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

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?

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

Ideas:

And check the set, look at the code.

Code:

structnode{BOOLUp,down,left,right;} a[2505];intM,n;Chargraph[ -][ -];intfa[2505];intFINDFA (intx) {    returnfa[x]==x?fa[x]:fa[x]=Findfa (fa[x]);}voidUnion (intXinty) {    intfx=FINDFA (x); intfy=Findfa (y); if(fx!=y) {FA[FX]=fy; }}intMain () { while(SCANF ("%d%d", &m,&n),m>0|| N>0) {Rep (I,0, M-1) scanf ("%s", Graph[i]); Rep (I,1, M*n) a[i].up=a[i].down=a[i].left=a[i].right=false; Rep (I,0, M-1) {Rep (J,0, N-1){                Switch(Graph[i][j]) { Case 'A': a[i*n+j+1].up=true; a[i*n+j+1].left=true; Break;  Case 'B': a[i*n+j+1].up=true; a[i*n+j+1].right=true; Break;  Case 'C': a[i*n+j+1].left=true; a[i*n+j+1].down=true; Break;  Case 'D': a[i*n+j+1].right=true; a[i*n+j+1].down=true; Break;  Case 'E': a[i*n+j+1].up=true; a[i*n+j+1].down=true; Break;  Case 'F': a[i*n+j+1].left=true; a[i*n+j+1].right=true; Break;  Case 'G': a[i*n+j+1].left=true; a[i*n+j+1].right=true; a[i*n+j+1].up=true; Break;  Case 'H': a[i*n+j+1].up=true; a[i*n+j+1].down=true; a[i*n+j+1].left=true; Break;  Case 'I': a[i*n+j+1].right=true; a[i*n+j+1].left=true; a[i*n+j+1].down=true; Break;  Case 'J': a[i*n+j+1].up=true; a[i*n+j+1].down=true; a[i*n+j+1].right=true; Break;  Case 'K': a[i*n+j+1].up=true; a[i*n+j+1].left=true; a[i*n+j+1].down=true; a[i*n+j+1].right=true; Break; default: Break; }}} rep (I,1, M*n) fa[i]=i; Rep (I,1, M) {Rep (J,1, N) {                intnow= (I-1) *n+J; if(j!=1){                    intlast1= (I-1) *n+j-1; if(A[last1].right &&a[now].left)                    {Union (Last1,now); }                }                if(i!=1){                    intLast2= (I-2) *n+J; if(A[last2].down &&a[now].up)                    {Union (Last2,now); } }}} Map<int,int>MP;        Mp.clear (); intans=0; Rep (I,1, m*N) {            intt=Findfa (i); if(mp[t]==0){                ++ans; Mp[t]=1; }} printf ("%d\n", ans); }    return 0;}

HDU 1198 Farm Irrigation (and collection)

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.