hdu 5706 GirlCat

GirlCat

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


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
 

Source
思路:遍历地图遇到g或者c就深搜去找剩下的字符(girl,cat);遍历就行。

代码:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int n,m;
char map[1001][1001];
char a[5]={"girl"};
char b[4]={"cat"};
int ans1,ans2;
int dir[4][2]={1,0,-1,0,0,1,0,-1};
int jude(int x,int y)
{
    if(x<1||x>n||y<1||y>m) return 0;
    return 1;
}
void dfs(int x,int y,int c)
{// printf("%d %d %d %c\n",x,y,c,a[c]);
  if(c==4)
  {
      ans1++;
      return ;
  }

  for(int i=0;i<4;i++)
  {
      int xx=x+dir[i][0];
      int yy=y+dir[i][1];
      //printf("s %d %d\n",xx,yy);
      if(!jude(xx,yy)) continue;
      if(map[xx][yy]==a[c])
      {
          dfs(xx,yy,c+1);
      }

  }
}
void dfs1(int x,int y,int c)
{// printf("%d %d %d %c\n",x,y,c,a[c]);
  if(c==3)
  {
      ans2++;
      return ;
  }

  for(int i=0;i<4;i++)
  {
      int xx=x+dir[i][0];
      int yy=y+dir[i][1];
      //printf("s %d %d\n",xx,yy);
      if(!jude(xx,yy)) continue;
      if(map[xx][yy]==b[c])
      {
          dfs1(xx,yy,c+1);
      }

  }
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d %d",&n,&m);
        for(int i=1;i<=n;i++)
        {
            scanf("%s",map[i]+1);
        }
        ans1=0,ans2=0;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
               if(map[i][j]=='g')
               {
                   dfs(i,j,1);
               }
             if(map[i][j]=='c')
               {
                   dfs1(i,j,1);
               }
            }

        }
        printf("%d %d\n",ans1,ans2);
    }
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值