hdu 1198 Farm Irrigation(15ms)

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

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

题目大意:农民有块矩形的地,是由若干块正方形的田地组成,每块地有每一块的水管路,总共有11种管道铺法,联通的管道不需要重复浇水,问最少要在几个点浇水

我的思路是用图来做一次遍历之后用并查集来储存关系

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cstring>
  4 #include<cmath>
  5 #include<cstdlib>
  6 #include<algorithm>
  7 using namespace std;
  8 #define maxm 55
  9
 10 bool p[11][4];
 11 int father[maxm*maxm],Map[maxm][maxm];
 12
 13 int find(int x)//因为数据最大为2500不会爆栈就用递归来路径压缩
 14 {
 15     if(father[x] == x) return x;
 16     return x = find(father[x]);
 17 }
 18
 19 void updata(int a,int b)
 20 {
 21     int x = find(a);
 22     int y = find(b);
 23     father[y] = x;
 24 }
 25
 26 int main()
 27 {
 28     int m,n;
 29     char s;
 30
 31     //初始化一哪里通了就在那里标记1
 32     memset(p,0,sizeof(p));
 33     p[0][0] = p[0][1] = 1;
 34     p[1][1] = p[1][2] = 1;
 35     p[2][0] = p[2][3] = 1;
 36     p[3][2] = p[3][3] = 1;
 37     p[4][1] = p[4][3] = 1;
 38     p[5][0] = p[5][2] = 1;
 39     p[6][0] = p[6][1] = p[6][2] = 1;
 40     p[7][0] = p[7][1] = p[7][3] = 1;
 41     p[8][0] = p[8][2] = p[8][3] = 1;
 42     p[9][1] = p[9][2] = p[9][3] = 1;
 43     p[10][0] = p[10][1] = p[10][2] = p[10][3] = 1;
 44      /*不理解的可以用这个代码来看下
 45     for(int i = 0; i < 11; i++)
 46     {
 47         printf("%c\n",i+‘A‘);
 48         cout<<‘ ‘<<p[i][1]<<endl;
 49         cout<<p[i][0]<<‘ ‘<<p[i][2]<<endl;
 50         cout<<‘ ‘<<p[i][3]<<endl;
 51         cout<<endl;
 52     }
 53     */
 54
 55     while(cin>>m>>n,n != -1)
 56     {
 57         for(int i = 0; i < m ; i++)
 58             for(int j = 0; j < n; j++)
 59             {
 60                 cin>>s;
 61                 Map[i][j] = s - ‘A‘;
 62             }
 63         int maxn = m*n;
 64         for(int i = 0; i < maxn; i++)
 65             father[i] = i;
 66         int x = m-1,y = n-1;
 67         //遍历
 68         for(int i = 0; i < x ; i++)
 69         {
 70             for(int j = 0; j < y; j++)
 71             {
 72                 if(p[Map[i][j]][3] == 1 && p[Map[i+1][j]][1] == 1)
 73                     updata(i*n+j,i*n+n+j);
 74                 if(p[Map[i][j]][2] == 1 && p[Map[i][j+1]][0] == 1)
 75                     updata(i*n+j,i*n+j+1);
 76             }
 77             if(p[Map[i][y]][3] == 1 && p[Map[i+1][y]][1] == 1)
 78                 updata(i*n+y,i*n+n+y);
 79         }
 80         for(int j = 0; j < y; j++)
 81         {
 82             if(p[Map[x][j]][2] == 1 && p[Map[x][j+1]][0] == 1)
 83                     updata(x*n+j,x*n+j+1);
 84         }
 85         //有种代码更简便的代码不过时间为45ms
 86         /*代码
 87         for(int i = 0; i < m ; i++)
 88             for(int j = 0; j < n; j++)
 89             {
 90                 if(i != m-1 && p[Map[i][j]][3] == 1 && p[Map[i+1][j]][1] == 1)
 91                     updata(i*n+j,i*n+n+j);
 92                 if(j != n-1 && p[Map[i][j]][2] == 1 && p[Map[i][j+1]][0] == 1)
 93                     updata(i*n+j,i*n+j+1);
 94             }
 95         */
 96         int ans = 0;
 97         for(int i = 0; i < maxn ; i++)
 98             if(father[i] == i) ans++;
 99         cout<<ans<<endl;
100     }
101     return 0;
102 }
103
104 /*
105 4 4
106 ACDA
107 BECF
108 IKAC
109 FCIJ
110
111 2 2
112 DK
113 HF
114
115 3 3
116 ADC
117 FJK
118 IHE
119
120 -1 -1
121 */

时间: 2024-11-08 18:23:36

hdu 1198 Farm Irrigation(15ms)的相关文章

hdu 1198 Farm Irrigation (搜索或并查集)

Farm Irrigation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5818    Accepted Submission(s): 2521 Problem Description Benny has a spacious farm land to irrigate. The farm land is a rectangle

hdu 1198 Farm Irrigation(深搜dfs || 并查集)

转载请注明出处:viewmode=contents">http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1198 ----------------------------------------------------------------------------------------------------------------------

HDU 1198 Farm Irrigation(并查集+位运算)

Farm Irrigation Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 38   Accepted Submission(s) : 24 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description Benny has a spacious

HDU 1198 Farm Irrigation(并查集,自己构造连通条件)

Farm Irrigation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 11188    Accepted Submission(s): 4876 Problem Description Benny has a spacious farm land to irrigate. The farm land is a rectangle

HDU 1198 Farm Irrigation (并查集优化,构图)

本题和HDU畅通工程类似,只不过畅通工程给出了数的连通关系, 而此题需要自己判断连通关系,即两个水管是否可以连接到一起,也是本题的难点所在. 记录状态,不断combine(),注意只需要判断左方和上方就行,这样不会重复判断,而且肯定都可以遍历到所有的状态. #include<stdio.h> #include<iostream> #include<string> //记录水管的形状,每种水管用一个由'0'和'1'组成的长度为4的字符串代表, //分别表示上下左右四边是否

HDU 1198 Farm Irrigation (并查集)

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 Description Benny has a spacious farm land to irrigate. The farm land is a rectangle,

HDU 1198 Farm Irrigation

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

【HDU1198】Farm Irrigation(回溯+记忆化搜索)

数据流小,深搜即可.有些暴力.看其他人的题解用二维转换成一维做的并查集很巧妙,马上去研究一下!! 1 #include <iostream> 2 #include <cstring> 3 #include <cstdlib> 4 #include <cstdio> 5 #include <cmath> 6 #include <cctype> 7 #include <algorithm> 8 #include <num

HDU 1728 逃离迷宫(BFS)

Problem Description 给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置,当然迷宫中有些地方是空地,gloria可以穿越,有些地方是障碍,她必须绕行,从迷宫的一个位置,只能走到与它相邻的4个位置中,当然在行走过程中,gloria不能走到迷宫外面去.令人头痛的是,gloria是个没什么方向感的人,因此,她在行走过程中,不能转太多弯了,否则她会晕倒的.我们假定给定的两个位置都是空地,初始时,gloria所面向的方向未定,她可