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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 为什么电脑压缩文件图标突然变成这样了

    为什么电脑压缩文件图标突然变成这样了

    2021年9月24日
    49
  • vue-router 基本使用「建议收藏」

    vue-router 基本使用「建议收藏」vue-router基本使用  路由,其实就是指向的意思,当我点击页面上的home按钮时,页面中就要显示home的内容,如果点击页面上的about按钮,页面中就要显示about的内容。Home按钮=>home内容,about按钮=>about内容,也可以说是一种映射.所以在页面上有两个部分,一个是点击部分,一个是点击之后,显示内容的部分。  点击之后,…

    2022年7月11日
    19
  • linux tomcat 安装教程,Linux安装配置Tomcat教程

    linux tomcat 安装教程,Linux安装配置Tomcat教程本文为你们分享了Linux安装配置Tomcat,供各位参考,具体内容如下1、下载tomcatLinux版本oracle官网下载地址:百度云盘链接:密码:5inw2、在usr目录下新建tomcat目录,然后将下载的tomcat用软件拷贝到这个新建的tomcat目录中3、解压tomcat文件由于文件名有点长,我们应该给解压后的文件夹重命名,以便下面引用方便4、配置环境变…

    2022年6月2日
    45
  • exit是什么意思(TerminateProcess)

    首先来谈谈一个进程的执行流程。每个应用程序都有个主函数,在WINDOWS下,只支持两种类型的应用程序——CUI(控制台应用程序)和GUI(图形界面应用程序),相应的,其主函数类型不同。来看下这几个入口函数int WINAPI WinMain(HINSTANCE hinstExe, HINSTANCE,PSTR pszCmdLine, int nCmdShow);    int WINAPT w

    2022年4月15日
    76
  • git如何合并远程2个分支

    git如何合并远程2个分支

    2021年10月22日
    96
  • java实现词法分析器

    java实现词法分析器实现词法分析器实验内容要求一 实验目的加深对词法分析器的工作过程的理解 加强对词法分析方法的掌握 能够采用一种编程语言实现简单的词法分析程序 能够使用自己编写的分析程序对简单的程序段进行词法分析 二 实验内容自定义一种程序设计语言 或者选择已有的一种高级语言 编制它的词法分析程序 词法分析程序的实现可以采用任何一种编程语言和编程工具 从输入的源程序中 识别出各个具有独立意义的单词 即关键字 标识符 常数 运算符 界符 并依次输出各个单词的内部编码及单词符号自身值 遇到错误时可显示 Er

    2025年9月20日
    5

发表回复

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

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