HDU-1198 farm irrigation and query set

Source: Internet
Author: User
Farm Irrigation

Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 1574 accepted submission (s): 704

Problem description

Benny
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
Distribution of water pipes over the whole farm. For example, if he has
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
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.

 

Input

There
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
Input, else you can assume 1 <= m, n <= 50.

Output

For each test case, output in one line the least number of wellsprings needed.

Sample input2 2
DK
HF

3 3
ADC
Fjk
IHE

-1-1

Sample output2
3

Just simulate the question. Be careful. Wa once, it is because when the coordinate is converted to the serial number, the "x-axis multiplied by column plus the upper column" is written as "x-axis multiplied by row plus column.
The basic idea is to set up four doors for each point, and "0/1" indicates whether the direction is successful. Note that the direction of the question must be unified, that is, the number of dir [] [] used to traverse the perimeter of a point. The numbers of top, bottom, and left correspond to 0, 1, and 2, respectively. Then, repeat it and find the number of root nodes. The Code is as follows:

# Include <stdio. h> # include <string. h> # include <stdlib. h> struct P {int num; int door [4];} p [55] [55]; int dir [4] [2] = {-1, 0, 1, 0, 0, 1, 0,-1}; // indicates the upper and lower left int pipe [11] [4] = {1, 0, 0, 1}, respectively }, {1, 0, 1, 0}, {0, 1, 0, 1}, {0, 1, 1, 0}, {1, 1, 0 }, {0, 0, 1, 1}, {1, 0, 1, 1}, {1, 1, 0, 1}, {0, 1, 1, 1 }, {1, 1, 1, 0}, {1, 1, 1 }}; int set [3000], M, N; int near (int x) {return x <= 1?! X :( x> 2? 2: 3);} int find (int x) {return set [x] = x = set [x]? X: Find (set [x]);} void merge (int x, int y) {int A = find (x), B = find (y ); set [a] = B;} void Update (int x, int y) {for (INT I = 0; I <4; ++ I) {int A = x + dir [I] [0], B = Y + dir [I] [1]; if (A <0 | A> = M | B <0 | B> = N) {continue;} int num1 = P [x] [Y]. num, num2 = P [a] [B]. num; If (find (num1 )! = Find (num2) {If (P [x] [Y]. door [I] & P [a] [B]. door [near (I)]) {// if the corresponding door is open, consider it as a region // printf ("num1 = % d, num2 = % d, I = % d \ n ", num1, num2, I); merge (num1, num2) ;}}} void Init (INT X, int y, char OP) {memset (P [x] [Y]. door, 0, sizeof (P [x] [Y]. door); P [x] [Y]. num = x * n + Y; For (INT I = 0; I <4; ++ I) {P [x] [Y]. door [I] + = pipe [Op-'a'] [I]; // printf ("Num [% d] = door [% d] = % d \ n", P [x] [Y]. num, I, P [x] [Y]. door [I]) ;}} int main () {While (scanf ("% d", & M, & N), N> 0 & M> 0) {for (INT I = 0; I <m * n; ++ I) {set [I] = I;} int ans = 0; For (INT I = 0; I <m; ++ I) {char STR [55], OP; scanf ("% s", STR); For (Int J = 0; j <N; ++ J) {op = STR [J]; Init (I, j, OP) ;}}for (INT I = 0; I <m; ++ I) {for (Int J = 0; j <n; ++ J) {Update (I, j) ;}} for (INT I = 0; I <n * m; ++ I) {If (set [I] = I) {ans ++ ;}} printf ("% d \ n", ANS );}}

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.