HDU 5706 GirlCat (DFS,暴力)

题意:给定一个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;
}
时间: 2024-10-21 20:25:46

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

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 - 简单搜索 - 有新手都可以看懂的详解

原题链接: 大致题意:给你一个二维字符串,可以看成图:再给两个子串"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(并且该

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

hdu 5024 Wang Xifeng&#39;s Little Plot (dfs+暴力)

Wang Xifeng's Little Plot Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 194    Accepted Submission(s): 131 Problem Description <Dream of the Red Chamber>(also <The Story of the Stone>)

hdu 1010 Tempter of the Bone(dfs暴力)

Problem Description The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it up, the maze began to shake, and the doggie could feel the ground sinking. He realized that the bone was a trap, and he tried despe

HDU 4499 Cannon (暴力搜索)

题意:在n*m的方格里有t个棋子,问最多能放多少个炮且每个炮不能互相攻击(炮吃炮) 炮吃炮:在同一行或同一列且中间有一颗棋子. #include <stdio.h> #include <iostream> #include <algorithm> #include <string.h> #include <queue> #include <math.h> #define M 50 #define LL long long using

Codeforces 6D Lizards and Basements 2 dfs+暴力

题目链接:点击打开链接 #include<stdio.h> #include<iostream> #include<string.h> #include<set> #include<vector> #include<map> #include<math.h> #include<queue> #include<string> #include<stdlib.h> #include<a

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

Board Game Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Gym 100935G Description standard input/outputStatements Feras bought to his nephew Saleem a new game to help him learning calculating. The game consists of a boar

NOIP 2002提高组 选数 dfs/暴力

1008 选数 2002年NOIP全国联赛普及组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 已知 n 个整数 x1,x2,…,xn,以及一个整数 k(k<n).从 n 个整数中任选 k 个整数相加,可分别得到一系列的和.例如当 n=4,k=3,4 个整数分别为 3,7,12,19 时,可得全部的组合与它们的和为: 3+7+12=22 3+7+19=29 7+12+19=38 3+12+19=34. 现在,要求你计算出和为素