hdu5706-GirlCat

Posted ljhaha

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hdu5706-GirlCat相关的知识,希望对你有一定的参考价值。

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.
1n1000 .
1m1000 .
(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
思路:给你一个二维字符串,可以看成图;再给两个子串“girl”和“cat”,求图中任意起点开始的不间断连接起来的字母构成的两个子串的分别的个数;连接的方向只有不间断的上下左右。
#include<cstdio>
#include<iostream>
using namespace std;
const int N=1010;
int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
char girl[]="girl";
char cat[]="cat";
char map[N][N];
int n,m,sumg,sumc;
void dfsgirl(int i,int j,int cnt)
{
    if(i<0||j<0||i>=n||j>=m)
        return;
    else{
        if(map[i][j]==girl[cnt]){
            if(cnt==3){
                sumg++;
                return;
            }
            dfsgirl(i-1,j,cnt+1);
            dfsgirl(i,j-1,cnt+1);
            dfsgirl(i+1,j,cnt+1);
            dfsgirl(i,j+1,cnt+1);
        }
    }
    return;
}
void dfscat(int i,int j,int cnt)
{
    if(i<0||j<0||i>=n||j>=m)
        return;
    else{
        if(map[i][j]==cat[cnt]){
            if(cnt==2){
                sumc++;
                return;
            }
            dfscat(i-1,j,cnt+1);
            dfscat(i,j-1,cnt+1);
            dfscat(i+1,j,cnt+1);
            dfscat(i,j+1,cnt+1);
        }
    }
    return;
}
int main()
{
    int t;scanf("%d",&t);
    while(t--){
        sumg=0,sumc=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){
                    dfsgirl(i,j,0);
                }
                if(map[i][j]==c){
                    dfscat(i,j,0);
                }
            }
        }
        printf("%d %d
",sumg,sumc);
    }
    return 0;
}

 

以上是关于hdu5706-GirlCat的主要内容,如果未能解决你的问题,请参考以下文章

hdu5706-GirlCat

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

HDU4057 Rescue the Rabbit(AC自动机+状压DP)

HDU3247 Resource Archiver(AC自动机+BFS+DP)

POJ - 2778 ~ HDU - 2243 AC自动机+矩阵快速幂

微信小程序代码片段