hdu 5706 GirlCat【暴力DFS】

原创 2016年05月30日 18:01:39

GirlCat

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

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

题目大意:有n*m个格子,从某一个格子出发,能向周围四个方向走,如果能够走到的地方能够组成单词girl,或者是cat都进行计数,最终输出其个数。


思路:枚举每个点,如果是g,深搜来找单词girl,如果是c,深搜来找单词cat,因为能够走到的地方要是上一个格子的字母的下一个字母,所以这里极大的限制了Dfs走的层数,也极大的限制了能够走到的地方的个数,所以是不会超时的,我刚看到这个题的时候 ,感觉好蛋疼,数量级很大啊,1000*1000的图还要递归找单词,其实不然,不会超时滴。


AC代码:

#include<stdio.h>
#include<string.h>
using namespace std;
char a[1005][1005];
int fx[4]={0,0,-1,1};
int fy[4]={1,-1,0,0};
int ans;
int n,m;
void Dfs(int x,int y)
{
    for(int i=0;i<4;i++)
    {
        int xx=x+fx[i];
        int yy=y+fy[i];
        if(xx>=0&&xx<n&&yy>=0&&yy<m)
        {
            if(a[x][y]=='g'&&a[xx][yy]=='i'||a[x][y]=='i'&&a[xx][yy]=='r')
            {
                Dfs(xx,yy);
            }
            if(a[x][y]=='r'&&a[xx][yy]=='l')ans++;
        }
    }
}
void Dfs2(int x,int y)
{
    for(int i=0;i<4;i++)
    {
        int xx=x+fx[i];
        int yy=y+fy[i];
        if(xx>=0&&x<n&&yy>=0&&yy<m)
        {
            if(a[x][y]=='c'&&a[xx][yy]=='a')
            {
                Dfs2(xx,yy);
            }
            if(a[x][y]=='a'&&a[xx][yy]=='t')
            {
                ans++;
            }
        }
    }
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        for(int i=0;i<n;i++)
        {
            scanf("%s",a[i]);
        }
        int output=0;
        int output2=0;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                if(a[i][j]=='g')
                {
                    ans=0;
                    Dfs(i,j);
                    output+=ans;
                }
                if(a[i][j]=='c')
                {
                    ans=0;
                    Dfs2(i,j);
                    output2+=ans;
                }
            }
        }
        printf("%d %d\n",output,output2);
    }
}







版权声明:0.0小白有写的不好不对的地方请指出~

HDU 2089 不要62(数位DP,三种姿势)

HDU 2089 不要62(数位DP,三种姿势) ACM 题目地址:HDU 2089 题意:  中文题意,不解释。 分析: 100w的数据,暴力打表能过先初始化dp数组...
  • hcbbt
  • hcbbt
  • 2014年07月26日 16:00
  • 6928

hdu5143 暴力枚举

http://acm.hdu.edu.cn/showproblem.php?pid=5143 Problem Description NPY is learning arithmeti...
  • u013573047
  • u013573047
  • 2014年12月14日 20:17
  • 1216

HDU1426 Sudoku Killer(深搜DFS)(很坑的一道题,解数独)

题目: Sudoku Killer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/...
  • riba2534
  • riba2534
  • 2016年12月08日 23:39
  • 1066

HDU5706 GirlCat(简单dfs)

HDU5706 GirlCat (简单dfs)Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768...
  • ping1632743560
  • ping1632743560
  • 2016年05月29日 23:13
  • 189

HDU 5706 GirlCat DFS解决

HDU 5706 GirlCat DFS
  • qq_26071477
  • qq_26071477
  • 2016年08月24日 19:52
  • 306

hdu 5706 GirlCat(dfs)

GirlCat Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...
  • qq_22522375
  • qq_22522375
  • 2016年06月10日 19:02
  • 296

HDU5706 GirlCat【DFS】

GirlCat Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...
  • tigerisland45
  • tigerisland45
  • 2016年07月03日 21:38
  • 2226

【2016杭电女生赛1005】【暴力 bfs or dfs or 模拟】girlcat 数女生数和猫咪数

#include #include #include #include #include #include #include #include #include #include #include #...
  • snowy_smile
  • snowy_smile
  • 2016年05月22日 22:35
  • 709

hdu 5547 Sudoku dfs 暴力回溯 解题报告

Problem Description Yi Sima was one of the best counselors of Cao Cao. He likes to play a funny g...
  • qq_21899803
  • qq_21899803
  • 2016年07月25日 14:21
  • 211

HDU 1258Sum It Up(暴力dfs,记住相同的状态只保留一个)

Sum It Up Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota...
  • opm777
  • opm777
  • 2013年10月17日 23:35
  • 1018
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:hdu 5706 GirlCat【暴力DFS】
举报原因:
原因补充:

(最多只允许输入30个字)