ZOJ - Farm Irrigation(并查集||DFS)
发布日期:2021-07-01 00:16:18 浏览次数:2 分类:技术文章

本文共 4603 字,大约阅读时间需要 15 分钟。

题目链接:

Time Limit: 2 Seconds Memory Limit: 65536 KB

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

ADCFJKIHE

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

Problem solving report:

Description: 有11种土地块,其中有修好的水渠,现在一片土地由这11种土地组成,需要浇水,问需要打多少口井。

Problem solving: 一开始首先想到的就是并查集,就是集合的合并,最后求多少个集合。我们只需要对每个土地块与右方和下方的进行合并即可。合并之前要先判断是否连通,若能连通则合并。另外一种就是DFS了,这个比较简单,类似于Oil Deposits问题().
并查集:

#include 
#define MAXN 55int n, m, ans, f[MAXN * MAXN];char map[MAXN][MAXN]; int dir[2][2] = {
{0, 1}, {1, 0}};int arr[] = {3, 6, 9, 12, 10, 5, 7, 11, 13, 14, 15};int getf(int v) { if (f[v] != v) return f[v] = getf(f[v]); return v;}bool merge(int u, int v) { int t1 = getf(u); int t2 = getf(v); if (t1 != t2) { f[t2] = t1; return true; } return false;}void edge(int x, int y) { int ta = map[x][y] - 'A'; for (int i = 0; i < 2; i++) { bool temp = false; int tx = x + dir[i][0]; int ty = y + dir[i][1]; if (tx >= n || ty >= m) continue; int tb = map[tx][ty] - 'A'; if (!i) { if (((arr[ta] >> 2) & 1) && (arr[tb] & 1)) temp = true; } else { if (((arr[ta] >> 3) & 1) && ((arr[tb] >> 1) & 1)) temp = true; } if (temp) if (merge(x * m + y, tx * m + ty)) ans++; }}int main() { while (scanf("%d%d", &n, &m), n > 0 && m > 0) { ans = 0; for (int i = 0; i < n * m; i++) f[i] = i; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) scanf(" %c", &map[i][j]); for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) edge(i, j); printf("%d\n", n * m - ans); } return 0;}

DFS:

#include 
#include
#define MAXN 55int n, m, ans, vis[MAXN][MAXN];char map[MAXN][MAXN]; int dir[4][2] = {
{0, 1}, {1, 0}, {0, -1}, {-1, 0}};int arr[] = {3, 6, 9, 12, 10, 5, 7, 11, 13, 14, 15};void DFS(int x, int y) { vis[x][y] = 1; int ta = map[x][y] - 'A'; for (int i = 0; i < 4; i++) { bool temp = false; int tx = x + dir[i][0]; int ty = y + dir[i][1]; if (tx >= n || tx < 0 || ty >= m || ty < 0 || vis[tx][ty]) continue; int tb = map[tx][ty] - 'A'; if (!i) { if (((arr[ta] >> 2) & 1) && (arr[tb] & 1)) temp = true; } else if (!(i - 1)){ if (((arr[ta] >> 3) & 1) && ((arr[tb] >> 1) & 1)) temp = true; } else if (!(i - 2)) { if ((arr[ta] & 1) && ((arr[tb] >> 2) & 1)) temp = true; } else { if (((arr[ta] >> 1) & 1) && ((arr[tb] >> 3) & 1)) temp = true; } if (temp) DFS(tx, ty); }}int main() { while (scanf("%d%d", &n, &m), n > 0 && m > 0) { ans = 0; memset(vis, 0, sizeof(vis)); for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) scanf(" %c", &map[i][j]); for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) if (!vis[i][j]) DFS(i, j), ans++; printf("%d\n", ans); } return 0;}

 

转载地址:https://lzyws739307453.blog.csdn.net/article/details/86686533 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:ZOJ - Red and Black(DFS)
下一篇:ZOJ - Rescue(BFS + 剪枝)

发表评论

最新留言

关注你微信了!
[***.104.42.241]2024年05月02日 21时18分49秒