Farm Irrigation
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7425 Accepted Submission(s): 3183
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.
![](https://i-blog.csdnimg.cn/blog_migrate/98438320b6a336844ea7a50507e0c94c.gif)
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
![](https://i-blog.csdnimg.cn/blog_migrate/c0c0d1bb80698cbbb549ba460e1ce196.gif)
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
一整块一搜,和一道dfs入门题差不多。
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<climits>
#include<queue>
#include<vector>
#include<map>
#include<sstream>
#include<set>
#include<stack>
#include<utility>
#pragma comment(linker, "/STACK:102400000,102400000")
#define PI 3.1415926535897932384626
#define eps 1e-10
#define sqr(x) ((x)*(x))
#define FOR0(i,n) for(int i=0 ;i<(n) ;i++)
#define FOR1(i,n) for(int i=1 ;i<=(n) ;i++)
#define FORD(i,n) for(int i=(n) ;i>=0 ;i--)
#define lson num<<1,le,mid
#define rson num<<1|1,mid+1,ri
#define MID int mid=(le+ri)>>1
#define zero(x)((x>0? x:-x)<1e-15)
#define mp make_pair
#define _f first
#define _s second
using namespace std;
const int INF =0x3f3f3f3f;
const int maxn=50+8 ;
//const int maxm= ;
//const int INF= ;
typedef long long ll;
const ll inf =1000000000000000;//1e15;
//ifstream fin("input.txt");
//ofstream fout("output.txt");
//fin.close();
//fout.close();
//freopen("a.in","r",stdin);
//freopen("a.out","w",stdout);
//by yskysker123
int n,m;
int dir[4][2]={{-1,0},{+1,0},{0,-1},{0,+1} };
vector<int> way[30];
int a[maxn][maxn];
bool vis[maxn][maxn];
bool in(int &y,int &x)
{
return 1<=y&&y<=n&&1<=x&&x<=m;
}
void dfs(int y,int x)
{
int k=a[y][x];
for(int i=0;i<way[k].size();i++)
{
int t=way[k][i];
int ty=y+dir[ t][0];
int tx=x+dir[ t][1];
/* cout<<"dfs "<<y<<" "<<x<<endl;
cout<<"t:"<<t<<endl;
cout<<ty<<" "<<tx<<endl;*/
if(!in(ty,tx)||vis[ty][tx] ) continue;
int id=a[ty][tx];
// cout<<"id "<<id<<endl;
int i_i;
for( i_i=0;i_i<way[id ].size();i_i++)
{
if( way[id][i_i] == (1^t) )
break;
}
if(i_i==way[id ].size()) continue;
vis[ty][tx]=1;
/*
cout<<"move"<<endl;
cout<<"now "<<y<<" "<<x<<endl;
cout<<"way "<<way[k][i]<<endl;
cout<<"nex"<<" "<<ty<<" "<<tx<<endl<<endl<<endl;*/
dfs(ty,tx);
}
}
int main()
{
int x='A'-'A';
way[x].push_back(0);
way[x].push_back(2);
x='B'-'A';
way[x].push_back(0);
way[x].push_back(3);
x='C'-'A';
way[x].push_back(1);
way[x].push_back(2);
x='D'-'A';
way[x].push_back(1);
way[x].push_back(3);
x='E'-'A';
way[x].push_back(0);
way[x].push_back(1);
x='F'-'A';
way[x].push_back(2);
way[x].push_back(3);
x='G'-'A';
way[x].push_back(0);
way[x].push_back(2);
way[x].push_back(3);
x='H'-'A';
way[x].push_back(0);
way[x].push_back(1);
way[x].push_back(2);
x='I'-'A';
way[x].push_back(1);
way[x].push_back(2);
way[x].push_back(3);
x='J'-'A';
way[x].push_back(0);
way[x].push_back(1);
way[x].push_back(3);
x='K'-'A';
way[x].push_back(0);
way[x].push_back(1);
way[x].push_back(2);
way[x].push_back(3);
while(~scanf("%d%d",&n,&m)&&n>=0&&m>=0)
{
char ch;
FOR1(i,n)
{
FOR1(j,m)
{
scanf(" %c",&ch);
a[i][j]=ch-'A';
}
}
memset(vis,0,sizeof vis);
int ans=0;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(vis[i][j]) continue;
vis[i][j]=1;
ans++;
// cout<<i<<" "<<j<<endl;
dfs(i,j);
}
}
printf("%d\n",ans);
}
return 0;
}
看到了用并查集做的,
并查集过后,判断有多少块的祖先节点和自己相同,就能知道有多少集合。
使用搜索就直接分成了一块一块的,但必须往四个方向搜索,
而并查集只用自上而下,自左向右考虑每一块能否与之前的连接就行了。
当然,考虑两个方向和四个方向差别并不大。