题干:

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

解题报告:

因为单词都很短,所以直接对于每一个起点bfs,长度超过5的时候就不再继续bfs了,这样保证复杂度是近似O(n*m)的。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define F first
#define S second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 2e5 + 5;
char s[1005][1005];
int nx[4] = {0,1,0,-1};
int ny[4] = {1,0,-1,0};
char nn[555];
int n,m;
struct Node {int x,y,t;char ch;Node(){}Node(int x,int y,int t,char ch):x(x),y(y),t(t),ch(ch){}
};
int bfs(int stx,int sty,char st,char tar) {int res = 0;queue<Node> q;q.push(Node(stx,sty,0,st));while(!q.empty()) {Node cur = q.front();q.pop();if(cur.ch == tar) {res++;continue;}if(cur.t > 5) continue;for(int k = 0; k<4; k++) {int tx = cur.x + nx[k];int ty = cur.y + ny[k];if(tx<1||tx>n||ty<1||ty>m) continue;if(s[tx][ty] == nn[cur.ch]) q.push(Node(tx,ty,cur.t+1,s[tx][ty]));}}return res;
}
int main()
{nn['g']='i';nn['i']='r';nn['r']='l';nn['c']='a';nn['a']='t';int t;cin>>t;while(t--) {ll ans1 = 0,ans2 = 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++) {if(s[i][j] == 'g') ans1 +=bfs(i,j,'g','l');}}for(int i = 1; i<=n; i++) {for(int j = 1; j<=m; j++) {if(s[i][j] == 'c') ans2 +=bfs(i,j,'c','t');}}printf("%lld %lld\n",ans1,ans2);} return 0 ;
}

【HDU - 5706】GirlCat(bfs)相关推荐

  1. 【HDU 1495】非常可乐(BFS)

    非常可乐 Description 大家一定觉的运动以后喝可乐是一件很惬意的事情,但是seeyou却不这么认为.因为每次当seeyou买了可乐以后,阿牛就要求和seeyou一起分享这一瓶可乐,而且一定要 ...

  2. 【HDU - 5965】扫雷(dp)

    题干: 扫雷游戏是晨晨和小璐特别喜欢的智力游戏,她俩最近沉迷其中无法自拔. 该游戏的界面是一个矩阵,矩阵中有些格子中有一个地雷,其余格子中没有地雷. 游戏中,格子可能处于己知和未知的状态.如果一个己知 ...

  3. 【HDU - 5777】domino(贪心)

    题干: Little White plays a game.There are n pieces of dominoes on the table in a row. He can choose a ...

  4. 【 HDU - 3062】Party(2-sat)

    题干: 有n对夫妻被邀请参加一个聚会,因为场地的问题,每对夫妻中只有1人可以列席.在2n 个人中,某些人之间有着很大的矛盾(当然夫妻之间是没有矛盾的),有矛盾的2个人是不会同时出现在聚会上的.有没有可 ...

  5. 【HDU - 1263】 水果(STL)

    题干: 夏天来了~~好开心啊,呵呵,好多好多水果~~  Joe经营着一个不大的水果店.他认为生存之道就是经营最受顾客欢迎的水果.现在他想要一份水果销售情况的明细表,这样Joe就可以很容易掌握所有水果的 ...

  6. 【HDU - 4345 】Permutation(DP)

    BUPT2017 wintertraining(15) #8F 题意 1到n的排列,经过几次置换(也是一个排列)回到原来的排列,就是循环了. 现在给n(<=1000),求循环周期的所有可能数. ...

  7. 【HDU - 1083 】Courses (二分图)

    题干: Consider a group of N students and P courses. Each student visits zero, one or more than one cou ...

  8. 【HDU - 5912】Fraction (模拟)

    题干: Mr. Frog recently studied how to add two fractions up, and he came up with an evil idea to troub ...

  9. 【HDU 5765】Bonds(进制运算妙用)

    [HDU 5765]Bonds(进制运算妙用) Bonds Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K ...

最新文章

  1. Codeforces Round #417:E. FountainsSagheer and Apple Tree(树上博弈)
  2. 腾讯面经 | 数据从业者的一次“典型”面试
  3. Coding: 一亿个数找最大的1000个数
  4. 内存泄露 内存溢出 内存碎片
  5. mybatis基础支撑层源码分析 日志模块需求
  6. python合并两个数据框_python 学习的第五天 数据框合并
  7. php 前置操作方法,前置操作-THINKPHP 5.0 手册最新版
  8. 【实践驱动开发3-001】TI WL1835MODCOM8 在android的移植 - 准备
  9. Python Flask Web 第二课 —— 请求-响应循环
  10. python常用内置模块,执行系统命令的模块
  11. 用AXIS2发布WebService的方法 使用eclipse插件生成服务端和客户端
  12. POJ-3267 The Cow Lexicon---删除字符匹配单词
  13. android开发点击版本号多次无法进入开发者模式模式
  14. v4l2驱动框架简介
  15. 通过Bottledwater同步PostgreSQL中的数据变化到Kafka消息队列
  16. 论文阅读:Automatic Landmark Estimation for Adolescent Idiopathic Scoliosis Assessment Using BoostNet
  17. 关注电动汽车能效水平 提高续航能力
  18. 解决ListView显示不全、滑动冲突问题
  19. 使用 Microsoft RDC for Mac 在 Mac 和 Windows 间传文件
  20. HTML5印章绘制电子签章图片,中文英文椭圆章、中文英文椭圆印章

热门文章

  1. 程序员九重镜界,很老的今天刚刚翻出来
  2. [Leetcode][第410题][JAVA][分割数组的最大值][动态规划][二分]
  3. 计算机二级1605错误,word 出现windows installer 1605错误
  4. java jxl 写 excel文件_Java使用jxl写入Excel文件
  5. micropython oled中文_micropython中怎么将gb2312编码的字节流变成中文
  6. outlook2013邮箱找不到服务器,outlook发邮件总提示找不到
  7. 专属海报小程序_剑3泡泡 | 小程序给你一份专属的账号海报!
  8. iptables nat实验_【零基础学云计算】LVS负载均衡群集之NAT模式搭建 (实践篇)...
  9. php post aspx,Login/LoginPost.aspx.vb 文件 LoginPost.aspx
  10. JS循环执行函数setInterval