hdu 5706 GirlCat 暴力

来源:互联网 发布:手机淘宝详情优惠券 编辑:程序博客网 时间:2024/05/17 08:32

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


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
31 4girl2 3otocat3 4girlhrlthlca
 

Sample Output
1 00 24 1
dfs 暴力
#include<stdio.h>#include<string.h>using namespace std;char s[1003][1003];int n,m;long long G,C;int dx[4]={0,0,-1,1};int dy[4]={1,-1,0,0};int dfs(int x,int y,int pos,int F){    for(int i=0;i<4;i++)    {        int tx=x+dx[i];        int ty=y+dy[i];        if(tx>0&&ty>0&&tx<=n&&ty<=m)        {            if(F)            {                if(pos==1&&s[tx][ty]=='i')                dfs(tx,ty,2,F);                if(pos==2&&s[tx][ty]=='r')                    dfs(tx,ty,3,F);                if(pos==3&&s[tx][ty]=='l')                    G++;            }            else {                if(pos==1&&s[tx][ty]=='a')                dfs(tx,ty,2,F);                if(pos==2&&s[tx][ty]=='t')                    C++;            }        }    }}int main(){    int T;    scanf("%d",&T);    while(T--)    {        C=0,G=0;        scanf("%d%d",&n,&m);        for(int i=1;i<=n;i++)            scanf("%s",s[i]+1);        for(int i=1;i<=n;i++)        {            for(int j=1;j<=m;j++)            {                //printf("%")                if(s[i][j]=='g')                    dfs(i,j,1,1);                if(s[i][j]=='c')                    dfs(i,j,1,0);            }        }        printf("%lld %lld\n",G,C);    }}


原创粉丝点击