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

InputThe 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. 
OutputAs 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<stdio.h>
#include<string.h>
#include<string>
#include<iostream>
#include<string.h>
#include<algorithm>
#include<vector>
#include<stdio.h>
#include<cstdio>
#include<time.h>
#include<stack>
#include<queue>
#include<deque>
#include<map>
#define inf 0x3f3f3f3f
#define ll long long
using namespace std;
char a[1005][1005];
int d[4][2]={{-1,0},{1,0},{0,1},{0,-1}};
struct node
{
    int x,y;
    char c;
};
queue<node>q;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n,m;
        cin>>n>>m;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                cin>>a[i][j];
            }
        }
        while(!q.empty ()) q.pop();
        int sg=0,sc=0;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                if(a[i][j]==‘g‘)
                {
                    node p;
                    p.x=i;
                    p.y=j;
                    p.c=‘g‘;
                    q.push(p);
                }
                if(a[i][j]==‘c‘)
                {
                    node p;
                    p.x=i;
                    p.y=j;
                    p.c=‘c‘;
                    q.push(p);
                }
            }
        }
        while(!q.empty())
        {
            node p=q.front ();
            q.pop();
            char c=p.c;
            for(int i=0;i<4;i++)
            {
                int xx=p.x+d[i][0];
                int yy=p.y+d[i][1];
                if(xx<1||yy<1||xx>n||yy>m) continue;
                char cc=a[xx][yy];
                if((c==‘g‘&&cc==‘i‘)||(c==‘i‘&&cc==‘r‘)||(c==‘c‘&&cc==‘a‘))
                {
                    node pp;
                    pp.x=xx;
                    pp.y=yy;
                    pp.c=cc;
                    q.push(pp);
                }
                if(c==‘r‘&&cc==‘l‘)
                {
                    sg++;
                }
                if(c==‘a‘&&cc==‘t‘)
                {
                    sc++;
                }
            }
        }
        cout<<sg<<" "<<sc<<endl;
    }
    return 0;

}

原文地址:https://www.cnblogs.com/caiyishuai/p/9033153.html

时间: 2024-11-09 00:31:17

hdu 5706 GirlCat(BFS)的相关文章

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,

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 - 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(并且该

[ACM] hdu 1242 Rescue (BFS+优先队列)

Rescue Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison. Angel's friends want to save Angel. Their task is:

hdu 4856 Tunnels(bfs+状态压缩)

题目链接:hdu 4856 Tunnels 题目大意:给定一张图,图上有M个管道,管道给定入口和出口,单向,现在有人想要体验下这M个管道,问最短需要移动的距离,起点未定. 解题思路:首先用bfs处理出两两管道之间移动的距离,然后后用状态压缩求出最短代价,dp[i][j],i表示的已经走过的管道,j是当前所在的管道. #include <cstdio> #include <cstring> #include <queue> #include <algorithm&g

hdu 1104 数论+bfs

题意:给n,m,k;输出n经过+-*%后(n%k+k)%k==((n+1)%k)%k  输出最小路径与运算副 zsd:% 只是求余数 有正负 mod 是求模 无正负. yhd:对m*k求余对 对k求余不对 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 5

HDU 1495——非常可乐( BFS )

非常可乐 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4663    Accepted Submission(s): 1868 Problem Description 大家一定觉的运动以后喝可乐是一件很惬意的事情,但是seeyou却不这么认为.因为每次当seeyou买了可乐以后,阿牛就要求和seeyou一起分享这一瓶可乐,而且一定要

hdu 5094 Maze bfs+状态压缩

Maze Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 100000/100000 K (Java/Others) Total Submission(s): 642    Accepted Submission(s): 229 Problem Description This story happened on the background of Star Trek. Spock, the deputy captain of St

hdu 1242 Rescue(bfs+优先队列)

Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison. Angel's friends want to save Angel. Their task is: approach Angel. We assume