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): 5809 accepted submission (s): 2516


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

 

Sample output23

 

Authorzheng, Lu

 

Sourcezhejiang University local contest 2005

 

Recommendignatius. L

 

This question refers to the number of connected blocks in the given graph.

Since it is a connected block, you can use and query the set to do it, but how to determine the connection situation should be considered here

I assume that each square has four directions. If this direction can be extended and connected to other squares, set this direction to 1; otherwise, set it to 0.

Then enumerate each square and merge it if it can be connected.

Count the number of root nodes.

1 # include <cstdio> 2 # include <cstring> 3 # include <stdlib. h> 4 # include <algorithm> 5 using namespace STD; 6 const int maxn = 55; 7 char ch; 8 int A [maxn] [maxn], P [maxn * maxn]; 9 int dir [4] [2] = {-}, {0,-1 }}; // search direction: top and bottom left 10 int map [11] [4] = }, // The connectivity conditions of each square are listed in sequence from top right to bottom left: 11 {,}, 12 {,}, 13 {,}, 14, }, 15 {,}, 16 {,}, 17 {,}, 18 {,}, 19 {,}, 20 {1, 1, 1} 21}; 22 int find (int x) 23 {24 return P [x] = x? X: P [x] = find (P [x]); 25} 26 27 void Union (int A, int B) 28 {29 int x = find (); 30 int y = find (B); 31 if (X! = Y) 32 P [x] = y; 33} 34 35 int main () 36 {37 // freopen ("in.txt", "r", stdin ); 38 int n, m; 39 while (scanf ("% d % * C", & M, & N) & M> 0) 40 {41 for (INT I = 0; I <m; I ++) 42 {43 for (Int J = 0; j <n; j ++) 44 {45 scanf ("% C", & Ch); 46 A [I] [J] = CH-'A'; 47} 48 getchar (); 49} 50 51 for (INT I = 0; I <n * m; I ++) 52 p [I] = I; 53 54 for (INT I = 0; I <m; I ++) 55 {56 for (Int J = 0; j <n; j ++) 57 {58 for (int K = 0; k <4; k ++) 59 {60 int next_x, next_y; 61 next_x = I + dir [k] [0]; 62 next_y = J + dir [k] [1]; 63 If (0 <= next_x & next_x <M & 0 <= next_y & next_y <n) 64 {65 if (k = 0) 66 {67 If (Map [A [next_x] [next_y] [2] & map [A [I] [J] [0]) 68 Union (next_x * n + next_y, I * n + J); 69} 70 If (k = 1) 71 {72 If (Map [A [next_x] [next_y] [3] & map [A [I] [J] [1]) 73 Union (next_x * n + next_y, I * n + J); 74} 75 if (k = 2) 76 {77 If (Map [A [next_x] [next_y] [0] & map [A [I] [J] [2]) 78 Union (next_x * n + next_y, I * n + J); 79} 80 If (k = 3) 81 {82 If (Map [A [next_x] [next_y] [1] & map [A [I] [J] [3]) 83 Union (next_x * n + next_y, I * n + J); 84} 85} 86} 87} 88} 89 // For (INT I = 0; I <n * m; I ++) 90 // printf ("% d", P [I]); 91 // printf ("\ n "); 92 93 int CNT = 0; 94 for (INT I = 0; I <n * m; I ++) 95 If (P [I] = I) 96 CNT ++; 97 printf ("% d \ n", CNT); 98} 99}
View code

 

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.