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

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

大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。

意甲冠军  两个农田管内可直接连接到壳体  他们将能够共享一个水源   有11种农田  管道的位置高于一定  一个农田矩阵  问至少须要多少水源

DFS的连通块问题  两个相邻农田的管道能够直接连接的话他们就属于一个连通块  题目就是求连通块个数 

#include<cstdio>
#include<cstring>
using namespace std;
const int N = 55;
char mat[N][N];
int type[11][4] = {   //相应11种水管类型 按顺时针方向有管道的为1否则为0
    {1, 0, 0, 1}, {1, 1, 0, 0}, {0, 0, 1, 1}, {0, 1, 1, 0},
    {1, 0, 1, 0}, {0, 1, 0, 1}, {1, 1, 0, 1}, {1, 0, 1, 1},
    {0, 1, 1, 1}, {1, 1, 1, 0}, {1, 1, 1, 1}
};

int dfs(int r, int c)
{
    int cur = mat[r][c] - 'A';
    if(cur < 0 || cur > 10) return 0;
    mat[r][c] = 0;           //标记已经訪问 0-'A'是小于0的
    int up = mat[r - 1][c] - 'A', dw = mat[r + 1][c] - 'A',
        le = mat[r][c - 1] - 'A', ri = mat[r][c + 1] - 'A';  //4个相邻块的管道类型
    if(up > -1 && up < 11 && type[up][2] && type[cur][0])  dfs(r - 1, c);
    if(dw > -1 && dw < 11 && type[dw][0] && type[cur][2])  dfs(r + 1, c);
    if(le > -1 && le < 11 && type[le][1] && type[cur][3])  dfs(r, c - 1);
    if(ri > -1 && ri < 11 && type[ri][3] && type[cur][1])  dfs(r, c + 1);
    return 1;     //一个连通块中仅仅有一个返回1
}

int main()
{
    int n, m, ans;
    while (scanf("%d%d", &n, &m), n >= 0)
    {
        ans = 0;
        memset(mat, 0, sizeof(mat));
        for(int i = 1; i <= n; ++i)
            scanf("%s", mat[i] + 1);
        for(int i = 1; i <= n; ++i)
            for(int j = 1; j <= m; ++j)
                ans += dfs(i, j);
        printf("%d\n", ans);
    }
    return 0;
}


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 different type of pipe. There are 11 types of pipes, which is marked from A to K, as Figure 1 shows.


ZOJ 2412 Farm Irrigation(DFS 条件通讯块)
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


ZOJ 2412 Farm Irrigation(DFS 条件通讯块)
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



版权声明:本文博客原创文章,博客,未经同意,不得转载。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/117484.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • vue-router 详解

    vue-router 详解文章目录1、认识vue-router2、安装和使用vue-router1、认识vue-router目前前端流行的三大框架,都有自己的路由实现:Angular的ngRouterReact的ReactRouterVue的vue-routervue-router是Vue.js官方的路由插件,它和vue.js是深度集成的,适合用于构建单页面应用。我们可以访问其官方网站对其进行学习:https://router.vuejs.org/zh/vue-router是基于路由和组件的路由用户设定访问

    2022年7月11日
    22
  • tfw格式图解[通俗易懂]

    tfw格式图解[通俗易懂]TFW格式,是关于TIFF影像坐标信息的文本文件。其它影像格式的坐标信息描述文件与其格式是一样的,后缀名可能不同。(bmp-bpw/png-pgw/jpg-jpw)话不多说,直接看图。上图中的UV坐标,实际上只的是图像的横向坐标和纵向坐标 。即图像的行和列坐标。 对于图上任意一个像素点(col,row)这个坐标,换算其地理坐标就十分简单。GeoX=1000.000+…

    2022年10月25日
    0
  • IDM 激活成功教程_idm中文激活成功教程版安卓

    IDM 激活成功教程_idm中文激活成功教程版安卓IDM确实好用,现贴出激活成功教程教程。感谢原文作者:https://jingyan.baidu.com/article/11c17a2c2bd026f447e39d5a.html转载于:https://www.cnblogs.com/zwz178/p/9472491.html

    2022年10月30日
    0
  • String数组的创建

    String数组的创建string数组的定义有三种写法:String[]arr=newString[10];//创建一个长度为10的String类型数组。Stringarr[]=newString[10];Stringarr[]={“张三”,”李四”};前面两种写法是一样的,可以互换,但是建议使用前者String[]arr因为java是强类型语言,声明…

    2022年6月7日
    114
  • Java的输入输出(新手上路版)

    Java的输入输出(新手上路版)

    2021年9月29日
    77
  • 微型计算机硬件性能主要取决于什么,微型计算机硬件系统的性能主要取决于

    微型计算机硬件性能主要取决于什么,微型计算机硬件系统的性能主要取决于大家好,我是时间财富网智能客服时间君,上述问题将由我为大家进行解答。微型计算机硬件系统的性能主要取决于微处理器。微处理器能完成取指令、执行指令,以及与外界存储器和逻辑部件交换信息等操作,是微型计算机的运算控制部分。它可与存储器和外围电路芯片组成微型计算机。微处理器,是指用一片或少数几片大规模集成电路组成的中央处理器。与传统的中央处理器相比,微处理器具有体积小、重量轻和容易模块化等优点。能完成取指令…

    2022年6月28日
    55

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号