题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1198
题目:
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
题目描述:
有图中几种形状的水管,给出一个n*m的矩阵,问最少需要多少水源点可以使整个图有水流通。题目乍一看有点难啊,不要自己把自己吓住了,仔细想想就发现其实可以写。写过这题后发现网上有人使用并查集来写的,哇,确实是啊,我怎么没想到呢?(个人认为搜索就是暴力,qwq)有兴趣可以看看并查集解法。
解题代码:
#include <stdio.h>
#include <string.h>
int a,b;
char d[110][110]; //存储地图
int bo[110][110]; //标记地图
void dfs(int x,int y)
{
int tx,ty;
if(bo[x][y]) //如果这个点使用过则返回
return ;
bo[x][y] = 1; //把这个点标记,表示这个点已经被使用过
if(d[x][y] == 'C' || d[x][y] == 'D' || d[x][y] == 'E' //如果水管口方向朝下 ,向下搜索
|| d[x][y] == 'H' || d[x][y] == 'I' || d[x][y] == 'J'
|| d[x][y] == 'K'){
tx = x + 1,ty = y;
if(tx >= 0 && tx < a && ty >= 0 && ty < b){ //判断数组下标是否越界
if(d[tx][ty] == 'A' || d[tx][ty] == 'B' || d[tx][ty] == 'E' //水管口方向朝上
|| d[tx][ty] == 'H' || d[tx][ty] == 'G' || d[tx][ty] == 'J'
|| d[tx][ty] == 'K'){
dfs(x + 1,y); //需要找水管口方向相反的进行接口
}
}
}
if(d[x][y] == 'C' || d[x][y] == 'A' || d[x][y] == 'F' //如果水管口方向朝左,向左搜索
|| d[x][y] == 'H' || d[x][y] == 'I' || d[x][y] == 'G'
|| d[x][y] == 'K'){
tx = x,ty = y - 1;
if(tx >= 0 && tx < a && ty >= 0 && ty < b){ //判断数组下标是否越界
if(d[tx][ty] == 'B' || d[tx][ty] == 'D' || d[tx][ty] == 'F' //水管口方向朝右
|| d[tx][ty] == 'G' || d[tx][ty] == 'I' || d[tx][ty] == 'J'
|| d[tx][ty] == 'K'){
dfs(x,y - 1); //需要找水管口方向相反的进行接口
}
}
}
if(d[x][y] == 'A' || d[x][y] == 'B' || d[x][y] == 'E' //如果水管口方向朝上 ,向上搜索
|| d[x][y] == 'H' || d[x][y] == 'G' || d[x][y] == 'J'
|| d[x][y] == 'K'){
tx = x - 1,ty = y;
if(tx >= 0 && tx < a && ty >= 0 && ty < b){ //判断数组下标是否越界
if(d[tx][ty] == 'C' || d[tx][ty] == 'D' || d[tx][ty] == 'E' //水管口方向朝下
|| d[tx][ty] == 'H' || d[tx][ty] == 'I' || d[tx][ty] == 'J'
|| d[tx][ty] == 'K'){
dfs(x - 1,y); //需要找水管口方向相反的进行接口
}
}
}
if(d[x][y] == 'B' || d[x][y] == 'D' || d[x][y] == 'F' //如果水管口方向朝右 ,向右搜索
|| d[x][y] == 'G' || d[x][y] == 'I' || d[x][y] == 'J'
|| d[x][y] == 'K'){
tx = x,ty = y + 1;
if(tx >= 0 && tx < a && ty >= 0 && ty < b){ //判断数组下标是否越界
if(d[tx][ty] == 'C' || d[tx][ty] == 'A' || d[tx][ty] == 'F' //水管口方向朝左
|| d[tx][ty] == 'H' || d[tx][ty] == 'I' || d[tx][ty] == 'G'
|| d[tx][ty] == 'K'){
dfs(x,y + 1); //需要找水管口方向相反的进行接口
}
}
}
return ;
}
int main()
{
int i,j,t,q;
while(scanf("%d%d",&a,&b), a >= 0 && b >= 0){
memset(d,'\0',sizeof(d));
memset(bo,0,sizeof(bo));
for(i = 0 ;i < a;i ++)
scanf("%s",d[i]);
for(t = i = 0 ;i < a;i ++) //遍历整个地图
for(j = 0;j < b;j ++)
if(bo[i][j] == 0){ //如果这个点没有被使用则进入搜索
t ++; //每进入一次则水源点加一 ,因为每次搜索都会把这个点的所有接口全部找出来,并把相应的bo数组置为1
dfs(i,j); //进入深搜
}
printf("%d\n",t);
}
return 0;
}