HDU-5706(DFS)
题目链接: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): 1823 Accepted Submission(s): 1091
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.
1≤n≤1000.
1≤m≤1000.
∑(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
题目大意:输入第一行表示多少个测试案例,然后跟着T组测试案例,每一组都先输入多少行,多少列。然后紧跟着就是n行m列字符。然后让你找这个图中有多少个girl和cat,比如girl你就从g字符开始从它的上下左右找i,找到i后,又从i的上下左右找r。一次如果能组成一个完整的girl就算一个。cat也是这样。
解题思路:
典型的dfs,bfs亦可。这里用的是dfs,用两个字符数组将girl和cat存起来,需要注意的是每次递归传值时,都要把字符数组的下标传进去,遍历四个方向时利用传进来的下标取字符串中的字符来比较。思路虽然简单,但还是要注意dfs的细节。
AC代码:
#include<cstdio>
int M[4][2] = {-1, 0, 1, 0, 0, 1, 0, -1};
char s1[] = "girl", s2[] = "cat";
char s[1010][1010];
int ans1, ans2;
int n, m;
void dfsgirl(int x, int y, int k)
{int dx, dy;for(int i = 0; i < 4; ++i){dx = x + M[i][0];dy = y + M[i][1];if(dx >= 0 && dx < n && dy >= 0 && dy < m && s[dx][dy] == s1[k]){k++;if(k == 4){ans1++;}dfsgirl(dx, dy, k);k--;}}
}void dfscat(int x, int y, int k)
{int dx, dy;for(int i = 0; i < 4; ++i){dx = x + M[i][0];dy = y + M[i][1];if(dx >= 0 && dx < n && dy >= 0 && dy < m && s[dx][dy] == s2[k]){k++;if(k == 3){ans2++;k--;}else{dfscat(dx, dy, k);k--;}}}
}int main()
{int t;scanf("%d", &t);while(t--){ans1 = ans2 = 0;scanf("%d%d", &n, &m);for(int i = 0; i < n; ++i)scanf("%s", s[i]);for(int i = 0; i < n; ++i)for(int j = 0; j < m; ++j){if(s[i][j] == 'g'){dfsgirl(i, j, 1);}if(s[i][j] == 'c'){dfscat(i, j, 1);}}printf("%d %d\n", ans1, ans2);}return 0;
}
HDU-5706(DFS)相关推荐
- hdu 4751(dfs染色)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4751 思路:构建新图,对于那些两点连双向边的,忽略,然后其余的都连双向边,于是在新图中,连边的点是能不 ...
- hdu 3560(dfs判环)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3560 思路:dfs判环,这里成环的条件是环中的每个点的出度和入度都为1,因此dfs的时候只须判断的相连 ...
- HDU 2212 DFS (伪·DFS)
链接 : http://acm.hdu.edu.cn/showproblem.php?pid=2212 本来是想练下DFS的,结果被题目标题坑了(HDU也搞标题党?),更坑的是题还这么水 直接输出四个 ...
- hdu 1175 DFS连连看
题意:中文题就不说了. 思路:DFS,关键是那个转折大于两次就不行的情况怎么深搜,可以枚举递归起始点的四个方向,如果在DFS中的方向K和递归中的不一样而且还满足深搜条件,那么转折次数就加一. 这道题W ...
- poj 1564 Sum It Up | zoj 1711 | hdu 1548 (dfs + 剪枝 or 判重)
Sum It Up Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Sub ...
- hdu 5188 dfs+二分
get了很多新技能 当时想到了用dfs,但是排序用的是限制时间排序,一直没搞出来. 正解: 二分用时,dfs判断,为了顺利进行做题,需要按照做题开始时间排序 还可以用dp 题意: 作为史上最强的刷子之 ...
- HDU 3699 DFS
A hard Aoshu Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 62768/32768 K (Java/Oth ...
- HDU - Reversi(dfs+水题)
题目链接:点击查看 题目大意:下棋游戏,简单描述一下规则就是,当轮到某一个颜色的棋子操作时,必须在可以吃掉对方棋子的地方下棋,所谓吃掉,就是下棋的地方可以和任意一个己方棋子可以连成一条直线,直线之中至 ...
- hdu 1760 DFS+博弈
0代表可放 1带表不能放 每次放一个2*2的方块 不能放者败 如果先手必胜则输出Yes 必胜态:从当前状态所能到达的状态中存在一个必败态 必败态:从当前状态所能达到的状态全部是必胜态 Sample I ...
- hdu 1518 dfs+剪枝
题目大意: 几根棒子能否组成一个正方形 Sample Input 3 //测试组数 4 1 1 1 1 //棒子数目以及每根棒子的长度 5 10 20 30 40 50 8 1 ...
最新文章
- idea、eclipse常用快捷键
- Android 的 SDK Manager 无法启动 闪退解决方法
- 利用NLTK进行分句分词
- React系列---Webpack环境搭建(二)不同环境不同配置
- c# datatable用法总结
- Go语言channel与select原理
- Linux “百变”秀:今天 Windows 95,明天 Mac OS 9
- hdu 2011 多项式求和 解题报告
- OPENWRT,爱快等软路由推荐
- 美丽杭州,魅力杭州:弘阳地产水城新时代
- 腾讯这次组织变革是过渡的
- 使用python实现win10系统和arduino usb串口通信
- Lumen 中对 Dingo API 异常接管并自定义响应结果
- 24第六季四小时首播进入倒计时!
- php 24字母和 数字进行转化
- ASP.NET项目上云实践——华为云DevCloud(图书管理系统)
- URL中的特殊格式进行转换
- 三角形公式 [重心, 内心, 外心, 垂心]
- 关于USART波特率、TIM的外设预分频值
- 统计|如何理解假设检验中单侧检验与双侧检验
热门文章
- Java数字反转(编程题)
- python2.7解释器安装教程_Python解释器安装教程以及环境变量配置
- python怎么输出图像测试_python pyautogui-不检测图像时的位置打印问题
- ajax post 空格变加号,Ajax Post数据加号变空格详解
- MATLAB1770太阳黑子,基于MATLAB的太阳黑子时间序列与仿真
- java简单工厂模式_Java 简单工厂模式
- 太空将成为数据中心冷却新前沿
- 卓越性能代码_编程语言性能实测,Go比Python更胜一筹?
- ML之回归预测:利用FSR/RiR/BasisExpand/ Lasso/DT/RF/GB算法对红酒品质wine数据集实现红酒口感评分预测(实数值评分预测)
- docker-compose 部署elk+解决时间不对导致kibana找不到logstash定义的index + docker-compose安装...