hdu1198 Farm Irrigation Dfs

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

Sample Output
23Ideas:traverse the map where you haven't been visited, use DFS to search and mark where you can get it. When you are aware of DFS, it is important to determine whether the current location and adjacent locations are interconnected to transfer code:
#include <iostream> #include <cstdio> #include <cstring> #include <queue>using namespace std; int dir[4][2]= {0,-1,-1,0,0,1,1,0}; Upper left right down clockwise int ok[11][4]= {{1,1,0,0},{0,1,1,0},{1,0,0,1},{0,0,1,1},{0,1,0,1},{1,0,1,0},{1,1,1,0},{1,1,0,1},{ 1,0,1,1},{0,1,1,1},{1,1,1,1}};    11 Blocks of 4 direction have no char map[55][55];int vis[55][55];int m,n;int t;void dfs (int x,int y) {int i,j,k; if (x<1| | y<1| | x>m| | y>n| |    Vis[x][y]) return;        if (t!=5)//is not the first level recursion to judge whether the former small block is connected to each other {k=map[x][y]-' A ';    if (ok[k][(t+2)%4]==0) return;    } vis[x][y]=1;    t=5;    k=map[x][y]-' A ';            for (i=0; i<4; i++) {if (ok[k][i]==1) {t=i;        DFS (x+dir[i][0],y+dir[i][1]); }} return;    int main () {int i,j,ans;        while (~SCANF ("%d%d", &m,&n) &&m+n!=-2) {ans=0;        memset (vis,0,sizeof (VIS));    for (I=1; i<=m; i++) for (j=1; j<=n; j + +) cin>>map[i][j];    For (I=1, i<=m; i++) for (j=1; j<=n; J + +) {if (!vis[i][j]) {                    t=5;                    ans++;                DFS (I,J);    }} cout<<ans<<endl; } return 0;}


hdu1198 Farm Irrigation Dfs

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.