HDU 5706 GirlCat DFS解决
来源:互联网 发布:网站主持人源码 编辑:程序博客网 时间:2024/05/17 09:10
GirlCat
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 383 Accepted Submission(s): 261
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 isn×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.
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
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 integersn and m , which are the height and the width of the photo.
Then there aren 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 .
As for each case, the first line are two integers
Then there are
It is guaranteed that:
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.
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
31 4girl2 3otocat3 4girlhrlthlca
Sample Output
1 00 24 1
Source
"巴卡斯杯" 中国大学生程序设计竞赛 - 女生专场
Recommend
liuyiding
题意:从输入的字符块中找出连续的"gril""cat"的个数。
分析:DFS ,BFS都能过。实现保存girl数组和cat数组,DFS过程中迭代结果数组下标即可。比完看题解,甚至暴力if循环都能过……见AC代码:
#include<stdio.h>#include<string.h>using namespace std;const int maxn = 1005;char girl[4]= {'g','i','r','l'};char cat[3]= {'c','a','t'};char map[maxn][maxn];int vis[maxn][maxn];int n,m;int numgirl,numcat;void dfs(int x,int y,int z){if(x<0||x>=n||y<0||y>=m||map[x][y]!=girl[z])return;vis[x][y]=1;if(z==3){numgirl++;return;}dfs(x+1,y,z+1);dfs(x,y+1,z+1);dfs(x-1,y,z+1);dfs(x,y-1,z+1);}void dfs2(int x,int y,int z){if(x<0||x>=n||y<0||y>=m||map[x][y]!=cat[z])//y> m 不是nreturn;vis[x][y]=1;if(z==2){numcat++;return;}dfs2(x+1,y,z+1);dfs2(x,y+1,z+1);dfs2(x-1,y,z+1);dfs2(x,y-1,z+1);}int main(){int T;scanf("%d",&T);while(T--){scanf("%d%d",&n,&m);numgirl=0,numcat=0;memset(vis,0,sizeof(vis));for(int i=0; i<n; i++)scanf("%s",map[i]);for(int i=0; i<n; i++)for(int j=0; j<m; j++)if(!vis[i][j]){if(map[i][j]=='g')dfs(i,j,0);else if(map[i][j]=='c')dfs2(i,j,0);}printf("%d %d\n",numgirl,numcat);}}DFS题还是需要多敲。
特记下,以备后日回顾。
0 0
- HDU 5706 GirlCat DFS解决
- hdu 5706 GirlCat(dfs)
- hdu 5706 GirlCat(DFS)
- hdu 5706 GirlCat【暴力DFS】
- hdu 5706 GirlCat(深搜)
- HDU 5706 GirlCat
- hdu 5706 GirlCat
- hdu 5706 GirlCat
- hdu 5706 GirlCat 暴力
- DFS-GirlCat
- HDU5706 GirlCat(简单dfs)
- HDU-5706-GirlCat【BFS】【2016CCPC女生专场】
- hdoj-5706-GirlCat
- 2016女生赛 1005 GirlCat【DFS】
- HDU ACM 1312 :DFS解决
- HDU 5706 暴力dfs
- HDU5706 GirlCat
- 【2016杭电女生赛1005】【暴力 bfs or dfs or 模拟】girlcat 数女生数和猫咪数
- Android中View绘制流程以及invalidate()等相关方法分析
- linux C语言学习之安装linux系统
- AndroidStudio无法预览布局文件问题的解决
- 李开复给大学生的第1封信:从诚信谈起
- ImageView中src与background的区别
- HDU 5706 GirlCat DFS解决
- hive 内置操作符
- 传输层TCP协议
- 【垂直搜索引擎搭建13】HtmlParser中Visitor实践
- 创建UITableViewCell程序奔溃
- eclipse如何安装genymotion插件,终于安装成功了,终极方案
- 集合框架的讲解--面试常问问题
- linux把标准输出赋值给变量遇到的问题
- 我的webpack配置文件