(Hdu step 5.1.4) Farm Irrigation (the number of collections in cases where two nodes are combined with restricted conditions)

Source: Internet
Author: User



Topic:

Farm Irrigation
Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 202 Accepted Submission (s): 104
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
Authorzheng, Lu
Sourcezhejiang University Local Contest 2005
Recommendignatius.l

Main topic:

There is a matrix, the tremor inside there are n*m a small square. Each block can be paved with a water pipe. In order for all small squares to be able to irrigate, ask for at least how many wells to hit.


Topic Analysis:

And check set, simple problem. In the previous question we did, if the "a B" data, it would represent the two nodes connected. But in

In this problem, the adjacent two nodes in map[][] are not necessarily connected and need to meet certain conditions to connect. For this n*m matrix, each node

It is only possible to merge in two directions (right and bottom).


The code is as follows:

/* * d.cpp * * Created on:2015 March 2 * author:administrator * * #include <iostream> #include <cstdio>using namespace Std;const int maxn = 51;int father[maxn*maxn];//is used to save the parent-child relationship Char map[maxn][maxn];//map information. such as map[0][0]= ' B '  That's the No. 0 row, No. 0 column. This small square is made of B this type of water pipe//used to mark the characteristics of various types of water pipes. 1 indicates a plumbing to int type[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} };bool VERTICAL = true;//Determines whether the int n,m;//n in the vertical direction: number of rows. M: Number of columns int cnt;//Collection/** * Determine if coordinates are valid */bool check (int x,int y) {if (x < 0 | | X >= N | | Y < 0 | | Y >= m) {//If the coordinates are already out of bounds return false;//returns False}return true;//return true}/** * Initialize parent-child relationship */void init () {int i;for (i = 0; i < MAXN *MAXN; ++i) {//Traversal of all nodes. It should be noted that if there is a problem, I try not to start from 0) father[i] = i;//All the fathers of the nodes are himself by default}}/** * Find the root node of the collection where a node */int find (int a) {if (a = = Father[a]) {// If the father of a node is its own return a;//then this node is the root node to be found}return father[a] = find (Father[a]);//continue to look for root node}/** * Merge two nodes * X1,y1: A node of the transverse longitudinal sittingSuperscript * X2,y2: The horizontal ordinate of the other node * dir: Used to mark whether the vertical direction */void join (int x1,int y1,int x2,int y2,bool dir) {if (check (x2,y2) = = False) {///if the second The coordinates of the node are not valid return;//Direct return}int T1 = map[x1][y1]-' a ';//Calculate the first node's water pipe type int t2 = map[x2][y2]-' a ';//calculate second node's water pipe type bool flag = Fals The e;//is used to mark whether the two nodes are connected, the default is Falseif (dir = = true) {//if the vertical direction//is the bottom of the first node and the second node above whether there is a pipe if (type[t1][3] = 1 && type[ T2][1] = = 1) {flag = true;//if any, flag flag as true, indicating that the two nodes are connected}}else{//if not perpendicular//Then the left of the node of the right and second node of the first nodes is judged if the colleague has a pipe if (type[ T1][2] = = 1 && type[t2][0] = = 1) {flag = true;//if any, flag flag as true, indicating that the two nodes are connected}}if (flag = = True) {//If two nodes are connected, Merge them with int a = X1*m + y1;//to convert the two-dimensional coordinates to one-dimensional coordinates int b = x2*m + y2;int FA = Find (a); int fb = find (b); if (FA! = FB) {FATHER[FA] = fb;// And two combined cnt--;//the total number of aggregates minus one}}}int main () {while (scanf ("%d%d", &n,&m)!=eof,n >= 0) {init ();//Initialize Parent-child relationship cnt = n*m;// Initializes the number of collections. The default is N*mint i;for (i = 0; i < n; ++i) {cin >> map[i];} int j;for (i = 0; i < n; ++i) {for (j = 0; j < m; ++j) {join (i,j,i+1,j,vertical);//merge one node and node below it join (I,j,i,j+1,! VERTICAL);//merge a node and its right node}}printf ("%d\n", CNT);} return 0;}








(Hdu step 5.1.4) Farm Irrigation (the number of collections in cases where two nodes are combined with restricted conditions)

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.