ZOJ - 2412 Farm Irrigation

本文概述了AI音视频处理领域的关键技术,包括视频分割、语义识别、自动驾驶、AR增强现实、SLAM、物体检测与识别、语音识别与变声等。探讨了这些技术在实际应用中的作用与价值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Time Limit: 2000MS Memory Limit: 65536KB 64bit IO Format: %lld & %llu

 Status

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


Source

Zhejiang University Local Contest 2005
 
 
 
 
分析:
图的表示+dfs。模板题。
ac代码:
//zoj2412
#include <iostream>
#include<cstdio>
using namespace std;
struct node
{
    int is;
    int left1,right1,up1,down1;
}mat[55][55];
struct node Change(char c)
{
    struct node t;
    t.down1=t.left1=t.right1=t.up1=0;
    if(c=='A')
    {
        t.left1=t.up1=1;
        return t;
    }
    if(c=='B')
    {
        t.right1=t.up1=1;
        return t;
    }
    if(c=='C')
    {
        t.left1=t.down1=1;
        return t;
    }
    if(c=='D')
    {
        t.right1=t.down1=1;
        return t;
    }
    if(c=='E')
    {
        t.up1=t.down1=1;
        return t;
    }
    if(c=='F')
    {
        t.left1=t.right1=1;
        return t;
    }
    if(c=='G')
    {
        t.left1=t.up1=t.right1=1;
        return t;
    }
    if(c=='H')
    {
        t.left1=t.up1=t.down1=1;
        return t;
    }
    if(c=='I')
    {
        t.left1=t.right1=t.down1=1;
        return t;
    }
    if(c=='J')
    {
        t.right1=t.up1=t.down1=1;
        return t;
    }
    if(c=='K')
    {
        t.left1=t.right1=t.up1=t.down1=1;
        return t;
    }
    //return t;//
}
void dfs(int x,int y)
{
    if(mat[x][y].up1&&mat[x-1][y].down1&&!mat[x-1][y].is)//!mat[x-1][y].is的判断很重要,否则出现Segmentation Fault (段错误),即堆栈耗费太大
    {
        mat[x-1][y].is=1;
        dfs(x-1,y);
    }
    if(mat[x][y].down1&&mat[x+1][y].up1&&!mat[x+1][y].is)
    {
        mat[x+1][y].is=1;
        dfs(x+1,y);
    }
    if(mat[x][y].left1&&mat[x][y-1].right1&&!mat[x][y-1].is)
    {
        mat[x][y-1].is=1;
        dfs(x,y-1);
    }
    if(mat[x][y].right1&&mat[x][y+1].left1&&!mat[x][y+1].is)
    {
        mat[x][y+1].is=1;
        dfs(x,y+1);
    }
}
int main()
{
    int i,j,m,n;
    char c;
    int count;
    while(scanf("%d%d",&m,&n)&&m>=0&&n>=0)
    {
        getchar();//读掉换行
        count=0;
        for(i=1;i<=m;i++)
        {
            for(j=1;j<=n;j++)
            {
                scanf("%c",&c);
                //printf("%c ",c);//
                mat[i][j]=Change(c);//把Change(c)的返回值赋给mat[i][j]
                mat[i][j].is=0;
                //cout<<mat[i][j].down1<<mat[i][j].left1<<" ";//通过这个找到了错误
            }
            getchar();
        }
        for(i=1;i<=m;i++)//
        {
            mat[i][1].left1=mat[i][n].right1=0;
        }
        for(i=1;i<=n;i++)//
        {
            mat[1][i].up1=mat[m][i].down1=0;
        }
        for(i=1;i<=m;i++)
        {
            for(j=1;j<=n;j++)
            {
                if(!mat[i][j].is)
                {
                    mat[i][j].is=1;
                    dfs(i,j);
                    count++;
                }
            }
        }
        printf("%d\n",count);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值