题意:给定一个n*m的矩阵,然后问你里面存在“girl”和“cat”的数量。

析:很简单么,就是普通搜索DFS,很少量。只要每一个字符对上就好,否则就结束。

代码如下:

#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
using namespace std ;
typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f3f;
const double eps = 1e-8;
const int maxn = 1000 + 5;
const int dr[] = {0, 0, -1, 1};
const int dc[] = {-1, 1, 0, 0};
char s[maxn][maxn];
int n, m;
int vis[maxn][maxn]; inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
} int dfs(int r, int c, char *t, int x, bool ok){
if(x == 3 && ok){
if(s[r][c] == t[x]) return 1;
return 0;
}
else if(!ok && x == 2){
if(s[r][c] == t[x]) return 1;
return 0;
} int ans = 0;
for(int i = 0; i < 4; ++i){
int xx = r + dr[i];
int yy = c + dc[i];
if(is_in(xx, yy) && s[xx][yy] == t[x+1]) ans += dfs(xx, yy, t, x+1, ok);
}
return ans;
} int main(){
int T; cin >> T;
while(T--){
scanf("%d %d", &n, &m);
for(int i = 0; i < n; ++i)
scanf("%s", s[i]); int ans1 = 0;
int ans2 = 0;
for(int i = 0; i < n; ++i){
for(int j = 0; j < m; ++j){
if(s[i][j] == 'g'){
ans1 += dfs(i, j, "girl", 0, true);
}
else if(s[i][j] == 'c')
ans2 += dfs(i, j, "cat", 0, false);
}
}
cout << ans1 << " " << ans2 << endl;
}
return 0;
}

HDU 5706 GirlCat (DFS,暴力)的更多相关文章

  1. HDU - 5706 - Girlcat - 简单搜索 - 新手都可以看懂的详解

    原题链接: 大致题意:给你一个二维字符串,可以看成图:再给两个子串“girl”和“cat”,求图中任意起点开始的不间断连接起来的字母构成的两个子串的分别的个数:连接的方向只有不间断的上下左右. 搜索函 ...

  2. hdu 5706 GirlCat(BFS)

    As a cute girl, Kotori likes playing ``Hide and Seek'' with cats particularly. Under the influence o ...

  3. hdu 5612 Baby Ming and Matrix games(dfs暴力)

    Problem Description These few days, Baby Ming is addicted to playing a matrix game. Given a n∗m matr ...

  4. hdu 1010 Tempter of the Bone(dfs暴力)

    Problem Description The doggie found a bone in an ancient maze, which fascinated him a lot. However, ...

  5. ACM: FZU 2107 Hua Rong Dao - DFS - 暴力

    FZU 2107 Hua Rong Dao Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I6 ...

  6. ACM: Gym 100935G Board Game - DFS暴力搜索

    Board Game Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u  Gym 100 ...

  7. HDU.5692 Snacks ( DFS序 线段树维护最大值 )

    HDU.5692 Snacks ( DFS序 线段树维护最大值 ) 题意分析 给出一颗树,节点标号为0-n,每个节点有一定权值,并且规定0号为根节点.有两种操作:操作一为询问,给出一个节点x,求从0号 ...

  8. NOIP 2002提高组 选数 dfs/暴力

    1008 选数 2002年NOIP全国联赛普及组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 已知 n 个整数 x1,x2,…, ...

  9. HDU 4431 Mahjong (DFS,暴力枚举,剪枝)

    题意:给定 13 张麻将牌,问你是不是“听”牌,如果是输出“听”哪张. 析:这个题,很明显的暴力,就是在原来的基础上再放上一张牌,看看是不是能胡,想法很简单,也比较好实现,结果就是TLE,一直TLE, ...

随机推荐

  1. Ubuntu中文输入法的安装

    Ubuntu上的输入法主要有小小输入平台(支持拼音/二笔/五笔等),Fcitx,Ibus,Scim等.其中Scim和Ibus是输入法框架. 在Ubuntu的中文系统中自带了中文输入法,通过Ctrl+S ...

  2. 解决mysql中文乱码(总)

    转自:http://hi.baidu.com/cnvienna/blog/item/2e99efdfd94fc61862279801.html MySQL会出现中文乱码的原因不外乎下列几点: .ser ...

  3. (六) 语言模型 Language Madel 与 word2vec

    语言模型简介(Language Model) 简单的说,语言模型 (Language Model) 是用来计算一个句子出现概率的模型,假设句子  ,其中  代表句子中的第  个词语,则语句 W 以该顺 ...

  4. 扩容盘、SD卡扩容

    内存卡的前世今生回想当年,大家都还在用着非智能机,由于功能单一,需要存储的数据也就是通讯录和短信.虽然那时也有手机游戏,但大多都是几十KB,并不需要太大的存储空间.但随着手机功能的多样化,尤其是音乐. ...

  5. [转]vi与vim的区别

    一直用着vi,有朋友劝我用vim,那么它们有什么区别呢? 简单点来说,它们都是多模式编辑器, 不同的是vim 是vi的升级版本,它不仅兼容vi的所有指令, 而且还有一些新的特性在里面. vim的这些优 ...

  6. window 7 下一台cp 两个mysql 配置主从

    环境 : 个人 pc windows7 一台 ; 使用 : 官方下载: mysql-noinstall-5.5.11-win32.zip 1. 解压 成2个 (文件夹) mysql_master (主 ...

  7. linearlayout 水平垂直居中

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools=&q ...

  8. js闭包用法

    闭包 既保证了 内部函数的私有性 又可以向外公开 通过一个已有对象 向它注入属性 /** * 闭包 * 在函数中定义的函数,在外部使用 * 1.在函数内部定义的函数,在外部不能访问 */ functi ...

  9. [转]Linux下which、whereis、locate、find 命令的区别

    转自:http://312788172.iteye.com/blog/730280 我们经常在linux要查找某个文件,但不知道放在哪里了,可以使用下面的一些命令来搜索.这些是从网上找到的资料,因为有 ...

  10. CListCtrl总结.xml

    pre{ line-height:1; color:#d1653c; background-color:#000000; font-size:16px;}.sysFunc{color:#566d68; ...