HDU5706 GirlCat【DFS】

 

GirlCat

 

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1074    Accepted Submission(s): 673

 

 

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×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

 

 

Source

"巴卡斯杯" 中国大学生程序设计竞赛 - 女生专场

 

 

 

问题链接HDU5706 GirlCat

问题简述:参见上述链接。

问题分析:(略)

程序说明

虽然是一个DFS问题,数据表示简单,用C语言编写程序。

给二维字符数组一个边界,程序逻辑就简洁多了,也许可以达到空间换时间的效果,空间换可阅读性是做到了。

C语言的二维数组是按行存放的,其每一行的首地址是可以获得的。有了边界之后,字符串放的位置需要简单调整一下。程序中是+1。

程序需要做的具有通用性,这也是值得注意的地方。本题是搜索“girl”和“cat”,如果换两个单词程序基本不用改,才能说程序有通用性。

功能能够封装则要尽量封装。C语言程序的功能要封装到函数里。

使用字符指针相信是一个好主意。

程序中使用了头文件"menory.h",改为"string.h"比较常见!

 

AC的C语言程序如下:

 

/* HDU5706 GirlCat */

#include <stdio.h>
#include <memory.h>

char a[1000+2][1000+2];
char p1[] = "girl";
char p2[] = "cat";
int count, count2;

void dfs(int row, int col, char *p)
{
    if(a[row][col] == *p) {
        if(*(p+1) == '\0')
            count++;
        else {
            p++;
            dfs(row-1, col, p);       // north
            dfs(row, col+1, p);       // east
            dfs(row+1, col, p);       // south
            dfs(row, col-1, p);       // west
        }
    }
}

int main(void)
{
    int t, n, m, i, j;

    scanf("%d", &t);
    while(t--) {
        // 二维字符数组清零
        memset(a, 0, sizeof(a));

        // 读入数据:二维字符数组,
        scanf("%d%d", &n, &m);
        for(i=1; i<=n; i++)
            scanf("%s", a[i]+1);

        count = 0;
        for(i=1; i<=n; i++)
            for(j=1; j<=m; j++)
                dfs(i, j, p1);      // 匹配girl并且计数

        count2 = count;
        count = 0;
        for(i=1; i<=n; i++)
            for(j=1; j<=m; j++)
                dfs(i, j, p2);      // 匹配cat并且计数

        // 输出结果
        printf("%d %d\n", count2, count);
    }

    return 0;
}

 

 

 

 

 

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值