(Hdu step 5.1.4) Farm Irrigation (calculate the number of sets when two nodes are merged with restrictions), hduirrigation

Source: Internet
Author: User

(Hdu step 5.1.4) Farm Irrigation (calculate the number of sets when two nodes are merged with restrictions), hduirrigation



Question:

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 has a spacious farm land to irrigate. the farm land is a rectangle, and is divided into a lot of samll squares. water pipes are placed in these squares. different square has a different type of pipe. there are 11 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 pipes over the whole farm. For example, if he has a map

ADC
FJK
IHE

Then the water pipes are distributed like


Figure 2

Several wellsprings are 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 will have a good harvest in autumn.

Now Benny wants to know at least how could wellsprings shocould be found to have the whole farm land irrigated. Can you help him?

Note: In the above example, at least 3 wellsprings are needed, as those red points in Figure 2 show.
 
InputThere are several test cases! In each test case, the first line contains 2 integers M and N, then M lines follow. in each of these lines, there are N characters, in the range of 'A' to 'k', denoting the type of water pipe over the corresponding 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

Question:

There is a matrix, which contains n * m small blocks. Each square can be paved with a pipe. In order to allow anyone in all small squares to get irrigation, find at least how many wells need to be drilled.


Question Analysis:

And query the set, simple question. In our previous questions, data such as "a B" indicates that the two nodes are connected. However

In this question, the adjacent two nodes in map [] [] are not necessarily connected. They must be connected only when certain conditions are met. For the n * m matrix, each node

You only need to determine whether the two directions can be merged (right and bottom ).


The Code is as follows:

/** D. cpp ** Created on: March 2, 2015 * Author: Administrator */# include <iostream> # include <cstdio> using namespace std; const int maxn = 51; int father [maxn * maxn]; // used to save the parent-child relationship char map [maxn] [maxn]; // map information. for example, map [0] [0] = 'B' indicates 0th rows and 0th columns. This small square uses a pipe of the B type. // It is used to identify various types of pipe.. 1 indicates that a pipe has been paved 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}, {1, 1, 1, 0}, {1, 1, 0, 1}, {1, 0, 1, 1}, {0, 1, 1, 1}, {1, 1, 1 }}; bool VERTICAL = true; // determines whether it is in the VERTICAL direction of int n, m; // n: number of rows. m: Number of columns int cnt; // number of sets/*** determines whether the coordinates are valid */bool check (int x, int y) {if (x <0 | x> = n | y <0 | y> = m) {// return false if the coordinates have crossed the border; // return false} return true; // return true}/*** initialize parent-child relationship */void init () {int I; for (I = 0; I <maxn * maxn; ++ I) {// traverses all nodes. (Note that I should not start from 0 if there is a problem) father [I] = I; // The Father of all nodes is his by default}/*** find one Root Node */int find (int a) {if (a = father [a]) {// if the parent of a node is its own return; // The node is the root node to be searched.} return father [a] = find (father [a]); // otherwise, continue searching for the root node}/*** merge two nodes ** x1, y1: the horizontal and vertical coordinates of one node * x2, y2: the horizontal and vertical coordinates of the other node * dir: used to mark whether it is vertical */void join (int x1, int y1, int x2, int y2, bool dir) {if (check (x2, y2) = false) {// If the coordinate of the second node is invalid return; // return directly} int t1 = map [x1] [y1]-'A '; // calculate the pipe type of the first node int t2 = map [x2] [y2]-'A'; // calculate the pipe type bool f of the second node Lag = false; // indicates whether two nodes are connected. The default value is falseif (dir = true) {// if the direction is vertical, // determine whether there is a pipe at the bottom of the first node and at the top of the second node. if (type [t1] [3] = 1 && type [t2] [1] = 1) {flag = true; // if both exist, flag is set to true, it indicates that the two nodes are connected} else {// if it is not the vertical direction // determine whether the right side of the first node and the left side of the second node have a pipe if (type [t1] [2] = 1 & type [t2] [0] = 1) {flag = true; // if both exist, flag is marked as true, indicating that the two nodes are connected} if (flag = true) {// if the two nodes are connected, merge them into int a = x1 * m + y1; // convert the two-dimensional coordinates into one-dimensional coordinates int B = x2 * m + y2; int fa = Find (a); int fb = find (B); if (fa! = Fb) {father [fa] = fb; // combine the two with cnt --; // subtract one from the total number of sets} int main () {while (scanf ("% d", & n, & m )! = EOF, n> = 0) {init (); // initialize the parent-child relationship cnt = n * m; // initialize the number of sets. The default value 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 a node and join (I, j, I, j + 1 ,! VERTICAL); // merge a node with the node on its right} printf ("% d \ n", cnt);} return 0 ;}








Related Article

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.