hdu 2952 Counting Sheep(简单dfs)
来源:互联网 发布:垃圾回收器 java 编辑:程序博客网 时间:2024/04/29 06:14
Counting Sheep
题目链接:点击打开链接题目大意:T组数据,每组数据第一行两个数H、W,H行W列包含 “.”草地 或 “#”羊 ,问有多少羊群(一只羊的上下左右四个方向如果有羊则算是一个羊群)。
解题思路:简单dfs遍历搜索,从第一个字符开始,如果判断是羊,羊群数tot++,并且对这只羊进行dfs(把其上下左右的羊都标记为草,因为这只算一个羊群),再进行下一个字符判断。
代码如下:
#include<iostream>#include<cstdio>#include<cstring>using namespace std;#define N 105char map[N][N];int go[4][2]={0,1,0,-1,1,0,-1,0};///四个方向判断int m,n;void dfs(int s,int t){ if(map[s][t]=='.') return ;///碰到不是羊,则结束此次搜索 map[s][t]='.';///开始对这只羊搜索,将这只羊标记为草 for(int i=0;i<4;i++){ int x=s+go[i][0],y=t+go[i][1]; if(x>=0&&x<m&&y>=0&&y<n&&map[x][y]=='#'){ ///如果旁边的依旧是羊,继续搜索 dfs(x,y); } }}int main(){ int ca; scanf("%d",&ca); while(ca--){ scanf("%d%d",&m,&n); for(int i=0;i<m;i++) scanf("%s",map[i]); int tot=0; for(int i=0;i<m;i++){ for(int j=0;j<n;j++){ if(map[i][j]=='#'){ tot++;///找到一个羊群 dfs(i,j); } } } printf("%d\n",tot); } return 0;}
0 0
- hdu 2952 Counting Sheep(简单dfs)
- HDU 2952 Counting Sheep 简单dfs
- HDU-2952 Counting Sheep (DFS)
- hdu 题目2952 Counting Sheep (DFS)
- hdu 2952 Counting Sheep (dfs)
- hdu 2952 Counting Sheep (DFS)
- HDU 2952 Counting Sheep (DFS)
- hdu 2952 Counting Sheep(dfs)
- HDU 2952 Counting Sheep(DFS)
- hdu 2952 Counting Sheep(dfs)
- HDU 2952 Counting Sheep [DFS]
- HDU 2952 Counting Sheep ( DFS + BFS )
- 2952 Counting Sheep【dfs】
- HDU--2592:Counting Sheep (DFS)
- HDOJ题目2952Counting Sheep (DFS)
- HDU 2952 Counting Sheep
- hdu 2952 Counting Sheep
- hdu-2952-Counting Sheep
- [水]ZOJ1666
- C++中引用(&)的用法和应用实例
- HDU 4923 - Room and Moor (贪心)
- 数据库的行式存储,列式存储
- 谷歌2012面试题-子串逆序打印
- hdu 2952 Counting Sheep(简单dfs)
- poj 2349 最小生成树
- 陈老师的多校联合20180808 map映射字符串
- 矩阵十题【六】 poj3070 Fibonacci
- hdu 3869 polya计数
- SIFT特征提取分析
- JVM规范学习:invokevirtual
- HDU 2852 KiKi's K-Number(线段树+二分)
- POJ2031 Building a Space Station(最小生成树,prim)