Reprint please indicate the source: http://blog.csdn.net/u012860063? Viewmode = Contents
Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1198
Certificate ----------------------------------------------------------------------------------------------------------------------------------------------------------
Welcome to tianzi Hut:Http://user.qzone.qq.com/593830943/main
Certificate ----------------------------------------------------------------------------------------------------------------------------------------------------------
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
There are 11 types of land blocks. The green lines in the blocks are the built water channels in the land blocks. Now, a piece of land is composed of the above various land blocks. You need to water them and ask how many wells are needed.
Code 1 (DFS ):
# Include <iostream> # include <algorithm> # include <cstring> using namespace STD; # define TM 117 // type of land stored in 11, 0 1 2 3 in two dimensions represents the upper left and lower right of this type of land. // 1 indicates that there is an interface in this direction, 0 indicates that no interface int A [11] [4] = {,}, {, 0}, {0,}, {0, }, {1, 1, 1}; int map [Tm] [Tm]; char s [Tm] [Tm]; bool vis [Tm] [Tm]; int n, m, coun; void DFS (int x, int y) {vis [x] [Y] = true; For (INT I = 0; I <4; I ++) {if (I = 0) {if (a [map [x] [Y] [0] & A [map [x-1] [Y] [2] & X-1> = 0 &&! Vis [x-1] [Y]) DFS (x-1, Y);} else if (I = 1) {if (a [map [x] [Y] [1] & A [map [x] [Y + 1] [3] & Y + 1 <m &&! Vis [x] [Y + 1]) DFS (X, Y + 1);} else if (I = 2) {if (a [map [x] [Y] [2] & A [map [x + 1] [Y] [0] & x + 1 <n &&! Vis [x + 1] [Y]) DFS (x + 1, Y);} else if (I = 3) {if (a [map [x] [y] [3] & A [map [x] [Y-1] [1] & Y-1> = 0 &&! Vis [x] [Y-1]) DFS (x, Y-1) ;}return ;}void Init () {memset (VIS, false, sizeof (VIS )); coun = 0;} int main () {int I, j; while (CIN> N> m) {Init (); if (n =-1 & M =-1) break; for (I = 0; I <n; I ++) {CIN> S [I]; for (j = 0; j <m; j ++) {map [I] [J] = s [I] [J]-'A ';}} for (I = 0; I <n; I ++) {for (j = 0; j <m; j ++) {If (! Vis [I] [J]) {coun ++; DFS (I, j) ;}}cout <coun <Endl ;}return 0 ;}
Code 2: (check the Set)
To be updated ............