码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 1198 Farm Irrigation

时间:2016-01-29 21:11:47      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:

Farm Irrigation

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7897    Accepted Submission(s): 3418


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 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 many wellsprings should 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 of 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 Input
2 2 DK HF 3 3 ADC FJK IHE -1 -1
 


Sample Output
2 3
 


Author
ZHENG, Lu
 


Source
 


Recommend

Ignatius.L



比较有意思的模拟题,对于一个方块,用四个bool变量标记方块四个边上是否能连通管道,检查与相邻的方块是否匹配。

 

 

 

 1 #include <cstdio>
 2 #include <algorithm>
 3 using namespace std;
 4 const int MAX = 60;
 5 struct Square{
 6     int p[4];//西北东南
 7 };
 8 const Square s[K - A + 1] = { { 1, 1, 0, 0 }, { 0, 1, 1, 0 }, { 1, 0, 0, 1 }, { 0, 0, 1, 1 },
 9                                   { 0, 1, 0, 1 }, { 1, 0, 1, 0 }, { 1, 1, 1, 0 }, { 1, 1, 0, 1 },
10                                   { 1, 0, 1, 1 }, { 0, 1, 1, 1 }, { 1, 1, 1, 1 } };
11 char farm[MAX][MAX];
12 bool t[MAX][MAX];
13 int f[MAX*MAX];
14 
15 int sf(int x)
16 {
17     return x == f[x] ? x : f[x] = sf(f[x]);
18 }
19 int m, n;
20 
21 int dx[4] = {0,1, 0,-1};
22 int dy[4] = {1,0,-1, 0};
23 void merge(int x, int y)
24 {
25     for (int i = 0, j = 2; i < 4; i++, j++, j %= 4)
26     {
27         int nx = x + dx[i];
28         int ny = y + dy[i];
29         if (0 <= nx && nx < m && 0 <= ny && ny < n && s[farm[nx][ny] - A].p[i]
30             && s[farm[x][y]-A].p[j]) //可以吻合
31         {
32             f[sf(nx*n + ny)] = sf(x*n + y);
33         }
34     }
35 }
36 
37 int main()
38 {
39     int ans;
40     while (scanf("%d%d", &m, &n) == 2 && n>0 && m>0)
41     {
42         ans = 0;
43         memset(t, 0, sizeof(t));
44         for (int i = 0; i<m; ++i)
45             scanf("%s", farm[i]);
46 
47         for (int i = 0, k = 0; i<m; ++i)
48             for (int j = 0; j<n; ++j, ++k)
49                 f[i*n + j] = k;
50 
51         for (int i = 0; i<m; ++i)
52             for (int j = 0; j<n; ++j)
53                 merge(i, j);
54         for (int i = 0; i<n*m; i++)
55         {
56             if (i == f[i])
57                 ans++;
58         }
59         /*
60         for (int i = 0; i<n*m; i++)
61         {
62             printf("%d ", f[i]);
63             if ((i + 1) % n == 0) printf("\n");
64         }
65         */
66         printf("%d\n", ans);
67     }
68 }

 

HDU 1198 Farm Irrigation

标签:

原文地址:http://www.cnblogs.com/cumulonimbus/p/5169937.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!