HDu 1198 Farm Irrigation

Source: Internet
Author: User

Farm IrrigationTime limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 7427 Accepted Submission (s): 3185


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 Input
2 2dkhf3 3adcfjkihe-1-1

Sample Output
23

Test instructions: Tell a few fields that if you have a pipeline, you can irrigate and ask for at least a few irrigation

Analysis, the main is to seek the Unicom block, a unicom block an irrigation device, direct demand for a few unicom block on it.

#include <iostream> #include <stdio.h> #include <string> #include <cstring> #include <cmath > #include <algorithm>using namespace Std;char s[1009][1009];int vis[1009][1009];int m,n;int dis[4][2]={0,1,0 , -1,1,0,-1,0};//right-left down int up[]={1,1,0,0,1,0,1,1,0,1,1};int dw[]={0,0,1,1,1,0,0,1,1,1,1};int le[]={    1,0,1,0,0,1,1,1,1,0,1};int ri[]={0,1,0,1,0,1,1,0,1,1,1};void dfs (int x,int y) {if (vis[x][y]) return;    Vis[x][y]=1;        for (int i=0;i<4;i++) {int xx=x+dis[i][0];        int yy=y+dis[i][1];        if (xx<0 | | xx>=m | | yy<0 | | yy>=n) continue;        if (i==0 && ri[s[x][y]-' a '] && le[s[xx][yy]-' a ']) DFS (XX,YY);        if (i==1 && le[s[x][y]-' a '] && ri[s[xx][yy]-' a ']) DFS (XX,YY);        if (i==2 && dw[s[x][y]-' a '] && up[s[xx][yy]-' a ']) DFS (XX,YY);    if (i==3 && up[s[x][y]-' a '] && dw[s[xx][yy]-' a ']) DFS (XX,YY);        }}int Main () {while (~scanf ("%d%d", &m,&n)) {if (n<0 | | m<0) break;        memset (vis,0,sizeof Vis);        for (int i=0;i<m;i++) scanf ("%s", S[i]);        int ans=0;                    for (int i=0;i<m;i++) for (int j=0;j<n;j++) if (vis[i][j]==0) {                    ans++;                DFS (I,J);    } printf ("%d\n", ans); } return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

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.