hdu 5706 GirlCat(深搜)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5706


——报考杭州电子科技大学!

GirlCat

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

Total Submission(s): 132    Accepted Submission(s): 104

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

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

题目大意:根据输入的字符矩阵,分别找到girl和cat字符串的数量。

解题思路:找到一个起始点,直接进行搜索,查找接下去的字母。

详见代码。

#include <iostream>
#include <cstdio>

using namespace std;

char Map[1010][1010];
char ans[2][10]= {{"cat"},{"girl"}};
int dir[4][2]= {0,1,0,-1,1,0,-1,0}; //4行2列
int n,m,s1,s2;

void dfs(int x,int y,int flag,int num)//flag用来标记我当前找的字符串是cat还是girl,num用来表示当前找到了是第几个字符。
{
    if (flag==1)//girl
    {

        if (num==3)//找到了girl的长度就加加
        {
            s1++;
            return;
        }
        for (int i=0; i<4; i++)
        {
            int X=x+dir[i][0];
            int Y=y+dir[i][1];
            if (X>=0&&X<n&&Y>=0&&Y<m&&(Map[X][Y]==ans[flag][num+1]))//一个字符一个字符比较,看是否为我接下去要找的那个字符
            {
                dfs(X,Y,1,num+1);
            }
        }
    }
    if (flag==0)
    {
        if (num==2)
        {
            s2++;
            return;
        }
        for (int i=0; i<4; i++)
        {
            int X=x+dir[i][0];
            int Y=y+dir[i][1];
            if (X>=0&&X<n&&Y>=0&&Y<m&&(Map[X][Y]==ans[flag][num+1]))
            {
                dfs(X,Y,0,num+1);
            }
        }
    }
}

int main()
{
    int t;
    scanf("%d",&t);
    while (t--)
    {
        s1=s2=0;
        scanf("%d%d",&n,&m);
        for (int i=0; i<n; i++)
        {
            scanf("%s",Map[i]);
        }
        for (int i=0; i<n; i++)
        {
            for (int j=0; j<m; j++)
            {
                if (Map[i][j]=='g')
                    dfs(i,j,1,0);
                if (Map[i][j]=='c')
                    dfs(i,j,0,0);
            }
        }
        printf ("%d %d\n",s1,s2);
    }
    return 0;
}
时间: 2024-10-01 05:01:09

hdu 5706 GirlCat(深搜)的相关文章

hdu 1518 Square 深搜,,,,花样剪枝啊!!!

Square Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 9588    Accepted Submission(s): 3127 Problem Description Given a set of sticks of various lengths, is it possible to join them end-to-end

HDU 5305 Friends(深搜)

题意:t组数据,每组一个n和m表示点数和边数,接下来m条边,每条边两种状态,求每个点邻接边的两种状态数目相同的排列方式有几种 分析:从第一个顶点开始往下深搜每条边,每条边两种状态,注意回朔. 代码: #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> using namespace std; const int maxn = 10; int n,m,ans; int e

HDU 5706 GirlCat (DFS,暴力)

题意:给定一个n*m的矩阵,然后问你里面存在“girl”和“cat”的数量. 析:很简单么,就是普通搜索DFS,很少量.只要每一个字符对上就好,否则就结束. 代码如下: #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #include <cstring> #include <set> #i

HDU 2614 Beat 深搜DFS

这道题目还是比较水的,但是题意理解确实费了半天劲,没办法 谁让自己是英渣呢! 题目大意: 猪脚要解决问题, 他有个习惯,每次只解决比之前解决过的问题的难度要大. 他给我们一个矩阵  矩阵的 i 行 j 列表示 解决完第 i 个问题后再解决第 j 个问题 花费时间为 T[i][j] 也就是 题目的难度. 并且他是从第0个问题开始解决的,第0个问题花费的时间为 0 下面是代码 : 1 #include<stdio.h> 2 #include<stdlib.h> 3 #include&l

HDU - 5706 - Girlcat - 简单搜索 - 有新手都可以看懂的详解

原题链接: 大致题意:给你一个二维字符串,可以看成图:再给两个子串"girl"和"cat",求图中任意起点开始的不间断连接起来的字母构成的两个子串的分别的个数:连接的方向只有不间断的上下左右. 搜索函数: void dfsgirl(int i, int j,int k); //第一层(i1,j1,  k=0)时,k=0表示(i1,j1)上是g,然后枚举四个方位找到i--进入第二层//第二层(i2,j2,   k=1)时,k=1表示(i2,j2)位置上是字母i(并且该

hdu 5706 GirlCat(BFS)

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

HDU 6264 (深搜,数论)

题目链接 题意 求\(\sum_{d|n}\phi (d) \times {n\over d}\),其中\(\phi(n) = n\prod_{p|n}({1-{1\over p}})\) 分析 将\(\phi(d)\) 分解式子代入可知:\(\sum_{d|n}(n\times \prod_{p|d}(1-{1\over p}))\) \(d\) 是 \(n\) 的因子,枚举 \(d\) 的质因子的所有可能的组成情况共\(2^c\)中. 其中 c 为 n 的不同质因子个数(即题目中输入的 n

深搜基础题目 杭电 HDU 1241

HDU 1241 是深搜算法的入门题目,递归实现. 原题目传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1241 代码仅供参考,c++实现: #include <iostream> using namespace std; char land[150][150]; int p,q; void dfs(int x,int y){ land[x][y] = '*'; if(land[x-1][y]!= '@' && land[x+1]

【深搜加剪枝五】HDU 1010 Tempter of the Bone

Tempter of the BoneTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 64326    Accepted Submission(s): 17567 Problem Description The doggie found a bone in an ancient maze, which fascinated him a l