hdu 5706 GirlCat(dfs)

  • Post author:
  • Post category:其他


GirlCat




Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 88    Accepted Submission(s): 71







Problem Description
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


×


m










, each pixel of the photo is a character of the lowercase(from `a’ to `z’).

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








T












which represents the case number.

As for each case, the first line are two integers








n










and








m










, which are the height and the width of the photo.

Then there are








n










lines followed, and there are








m










characters of each line, which are the the details of the photo.

It is guaranteed that:









T












is about 50.









1





n





1000










.









1





m





1000










.












(


n


×


m


)





2


×





10






6















.


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

#include<cstdio>
using namespace std;
char g[5] = { "girl" }, c[4] = { "cat" };
int dx[4] = { -1, 0, 0, 1 }, dy[4] = { 0, -1, 1, 0 };
char map[1200][1200];
int n, m;
int ans1, ans2;
void dfs_girl(int i, int j, int pos)
{
    if (pos == 4)
    {
        ans1++;    return;
    }
    for (int k = 0; k < 4; k++)
    {
        int x = i + dx[k], y = j + dy[k];
        if (x<1 || y<1 || x>n || y>m)continue;
        if (map[x][y] == g[pos])
            dfs_girl(x, y, pos + 1);
    }
}
void dfs_cat(int i, int j, int pos)
{
    if (pos == 3)
    {
        ans2++;    return;
    }
    for (int k = 0; k < 4; k++)
    {
        int x = i + dx[k], y = j + dy[k];
        if (x<1 || y<1 || x>n || y>m)continue;
        if (map[x][y] == c[pos])
            dfs_cat(x, y, pos + 1);
    }
}
int main()
{
    int t;
    scanf("%d", &t);
    while (t--)
    {
        ans1 = 0, ans2 = 0;
        scanf("%d%d", &n, &m);
        for (int i = 1; i <= n; i++)
            scanf("%s", map[i] + 1);
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= m; j++)
            {
                if (map[i][j] == 'g')
                    dfs_girl(i, j, 1);
                if (map[i][j] == 'c')
                    dfs_cat(i, j, 1);
            }
        printf("%d %d\n", ans1, ans2);
    }
    return 0;
}



版权声明:本文为qq_22522375原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。