E - GirlCat

As a cute girl, Kotori likes playing Hide and Seek'' with cats particularly.
Under the influence of Kotori, many girls and cats are playing
Hide and Seek” together.
Koroti shots a photo. The size of this photo is n×mn×m, each pixel of the photo is a character of the lowercase(from a' toz’).
Kotori wants to know how many girls and how many cats are there in the photo.

We define a girl as – we choose a point as the start, passing by 4 different connected points continuously, and the four characters are exactly girl'' in the order.
We define two girls are different if there is at least a point of the two girls are different.
We define a cat as -- we choose a point as the start, passing by 3 different connected points continuously, and the three characters are exactly
cat” in the order.
We define two cats are different if there is at least a point of the two cats are different.

Two points are regarded to be connected if and only if they share a common edge.
Input
The first line is an integer TT which represents the case number.

As for each case, the first line are two integers nn and mm, which are the height and the width of the photo.
Then there are nn lines followed, and there are mm characters of each line, which are the the details of the photo.

It is guaranteed that:
TT is about 50.
1≤n≤10001≤n≤1000.
1≤m≤10001≤m≤1000.
∑(n×m)≤2×106∑(n×m)≤2×106.
Output
As for each case, you need to output a single line.
There should be 2 integers in the line with a blank between them representing the number of girls and cats respectively.

Please make sure that there is no extra blank.

Sample Input
3
1 4
girl
2 3
oto
cat
3 4
girl
hrlt
hlca
Sample Output
1 0
0 2
4 1

题意:给你一张含有图,找cat 和 girl 有多少个(只要相邻就行 而且有一个字母没用过就算一个)
方法:dfs

#include <stdio.h>
#include <string.h>
char a[1004][1004];
int pos[1000004];
int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};
int sumcat,sumgirl;
int n,m;
int judge2(int x, int y, int tempx, int tempy)
{
    if(a[x][y] == 'g'&&a[tempx][tempy] =='i') return 1;
    if(a[x][y] == 'i'&&a[tempx][tempy] =='r') return 1;
    if(a[x][y] == 'c'&&a[tempx][tempy] =='a') return 1;
    return 0;
}
int judge(int x,int y,int tempx,int tempy)
{
    if((tempx>=0&&tempx<n&&tempy>=0&&tempy<m))
       {
           if(judge2(x,y,tempx,tempy))
           return 1;
           else return 0;
       }
    else return 0;
}
void dfs(int num)
{
    int x = num / m;
    int y = num % m;
    for(int i = 0;i < 4; i++)
    {
        int tempx = x + dx[i];
        int tempy = y + dy[i];
        if(tempx>=0&&tempx<n&&tempy>=0&&tempy<m&&a[x][y]=='a'&&a[tempx][tempy] == 't')
        {
            sumcat++;
            continue;
        }
        if(tempx>=0&&tempx<n&&tempy>=0&&tempy<m&&a[x][y]=='r'&&a[tempx][tempy] == 'l')
        {
            sumgirl++;
            continue;
        }
        if(judge(x,y,tempx,tempy))
        {
            dfs(tempx*m+tempy);
        }
    }
    return ;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        memset(pos,0,sizeof(pos));
        scanf("%d%d",&n,&m);
        sumgirl = 0;
        sumcat = 0;
        int p = 0;
        for(int i = 0; i < n; i++)
        {
            for(int j = 0; j < m; j++)
            {
                scanf(" %c",&a[i][j]);
                if(a[i][j] == 'c' || a[i][j] == 'g')
                {
                    pos[p++] = i*m + j;
                }
            }
        }
        for(int i = 0; i < p; i++)
        {
            dfs(pos[i]);
        }
        printf("%d %d\n",sumgirl,sumcat);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值