hdu 5706 DFS

 

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 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 TT which represents the case number. 

As for each case, the first line are two integers nn and mm, which are the height and the width of the photo. 
Then there are nn lines followed, and there are mm characters of each line, which are the the details of the photo. 

It is guaranteed that: 
TT is about 50. 
1≤n≤10001≤n≤1000. 
1≤m≤10001≤m≤1000. 
∑(n×m)≤2×106∑(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

寻找cat与girl单词。我们先找到‘c’与‘g’开头的位置,放入队列中。再dfs寻找即可。因为这里单词数已知,dfs的结束条件就是当前的位置走到了‘t’与‘l’时。

#include <queue>
#include <cstdio>
#include <set>
#include <string>
#include <stack>
#include <cmath>
#include <climits>
#include <map>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <stdio.h>
#include <ctype.h>
#include <bitset>
#include <time.h>
#define LL long long
#define ULL unsigned long long
#define mod 1000000007
#define INF 0x3ffffff
#define mem(a,b) memset(a,b,sizeof(a))
#define MODD(a,b) (((a%b)+b)%b)
#define PI 3.1415927
#define maxn 10005
using namespace std;
char mp[maxn][maxn];
int dx[4] = {0,1,-1,0};
int dy[4] = {1,0,0,-1};
int n,m,flag1 = 0,flag2 = 0;
char gg[5] = {"girl"};
char cc[4] = {"cat"};
struct node
{
    int x,y;
};
void dfs(int x,int y,int ans)
{
    if(x < 0||x >=n||y < 0||y >= m) return ;
    if(mp[x][y]!=gg[ans]) return ;
    if(ans == 3){
        flag1 ++;
        return ;
    }
    for(int i = 0; i < 4; i++){
        dfs(x + dx[i],y + dy[i],ans + 1);
    }
}
void dfsc(int x,int y,int ans)
{
    if(x < 0||x >=n||y < 0||y >= m) return ;
    if(mp[x][y]!=cc[ans]) return ;
    if(ans == 2){
        flag2 ++;
        return ;
    }
    for(int i = 0; i < 4; i++){
        dfsc(x + dx[i],y + dy[i],ans + 1);
    }
}
queue <node> g;
queue <node> c;
int main()
{
    int T;
    scanf("%d",&T);
    while(T--){
        flag1 = flag2 = 0;
        scanf("%d%d",&n,&m);
        for(int i = 0; i < n; i++){
            getchar();
            for(int j = 0; j < m; j++){
                scanf("%c",&mp[i][j]);
                if(mp[i][j] == 'c'){
                    node q;
                    q.x = i;
                    q.y = j;
                    c.push(q);

                }
                else if(mp[i][j] == 'g'){
                    node q;
                    q.x = i;
                    q.y = j;
                    g.push(q);

                }
            }
        }
        int ans = 0;
        int cnt = 0;
        while(!g.empty()){
            node p = g.front();
            g.pop();
            dfs(p.x,p.y,ans);
        }
        while(!c.empty()){
            node p = c.front();
            c.pop();
            dfsc(p.x,p.y,cnt);
        }
        printf("%d %d\n",flag1,flag2);

    }

    return 0;

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值