hdu1198 Farm Irrigation dfs

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

思路:
遍历地图  没被访问过的地方用dfs搜一遍并标记能达到的地方
注意dfs的时候要判断当前位置与相邻位置是否互相连通 需要互相连通才能转移

代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
int dir[4][2]= {0,-1,-1,0,0,1,1,0}; //左 上 右 下 顺时针
int ok[11][4]= {{1,1,0,0},{0,1,1,0},{1,0,0,1},{0,0,1,1},{0,1,0,1},{1,0,1,0},{1,1,1,0},{1,1,0,1},{1,0,1,1},{0,1,1,1},{1,1,1,1}}; //11块的4方向有无
char map[55][55];
int vis[55][55];
int m,n;
int t;
void dfs(int x,int y)
{
    int i,j,k;
    if(x<1||y<1||x>m||y>n||vis[x][y])
        return;
    if(t!=5)     // 不是第一层递归  要判断与前小块是否互相连通
    {
        k=map[x][y]-'A';
        if(ok[k][(t+2)%4]==0)
            return;
    }
    vis[x][y]=1;
    t=5;
    k=map[x][y]-'A';
    for(i=0; i<4; i++)
    {
        if(ok[k][i]==1)
        {
            t=i;
            dfs(x+dir[i][0],y+dir[i][1]);
        }
    }
    return;
}
int main()
{
    int i,j,ans;
    while(~scanf("%d%d",&m,&n)&&m+n!=-2)
    {
        ans=0;
        memset(vis,0,sizeof(vis));
        for(i=1; i<=m; i++)
            for(j=1; j<=n; j++)
                cin>>map[i][j];
        for(i=1; i<=m; i++)
            for(j=1; j<=n; j++)
            {
                if(!vis[i][j])
                {
                    t=5;
                    ans++;
                    dfs(i,j);
                }
            }
        cout<<ans<<endl;
    }
    return 0;
}

时间: 2024-08-03 13:50:32

hdu1198 Farm Irrigation dfs的相关文章

ZOJ 2412 Farm Irrigation(DFS 条件连通块)

题意  两块农田里面的管道可以直接连接的话  他们就可以共用一个水源   有11种农田  上面的管道位置是一定的  给你一个农田矩阵  问至少需要多少水源 DFS的连通块问题  两个相邻农田的管道可以直接连接的话他们就属于一个连通块  题目就是求连通块个数 #include<cstdio> #include<cstring> using namespace std; const int N = 55; char mat[N][N]; int type[11][4] = { //对应

HDU--1198 Farm Irrigation (并查集做法+DFS做法)

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 pi

【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

HDU1198水管并查集Farm Irrigation

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

ZOJ 2412 Farm Irrigation

Farm Irrigation Time Limit: 2 Seconds      Memory Limit: 65536 KB 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 di

hdu1198Farm Irrigation(dfs找联通)

题目链接: 啊哈哈,选我选我 思路是:首先根据图像抽象出联通关系..首先确定每一种图形的联通关系,用01值表示不连通与不连通...然后从第1个图形进行dfs搜索.如果碰到两快田地可以联通的话那么标记..注意处理的过程中你的 搜索顺序要和你的每个图形的连通性的顺序相同..然后就是最后看上下.左右能否匹配...看最后有几个不同的快,这就是答案,感觉跟并查集一样..并查集应该也可以做.. 题目: Farm Irrigation Time Limit: 2000/1000 MS (Java/Others

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

Farm Irrigation

题目:Farm Irrigation 题目链接:http://210.34.193.66:8080/vj/Problem.jsp?pid=1494 题目思路:并查集 1 #include<stdio.h> 2 //并查集重点就是实现:查询某点在哪个集合.将两个集合合并 3 //查找点的祖先 4 int a[10000]; 5 int fun(int x) 6 { 7 int p=x; 8 // 初始化数组 a[i]=i; 9 // 也就是说当 a[i]==i 时,这是一个祖先,或者他是别人的子

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