The number of ponds——DFS
来源:互联网 发布:js new cookie 编辑:程序博客网 时间:2024/05/16 15:16
地址:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=62709#problem/B
Description
Given a diagram of Farmer John's field, determine how many ponds he has.
Input
* Lines 2..N+1: M characters per line representing one row of Farmer John's field. Each character is either 'W' or '.'. The characters do not have spaces between them.
Output
Sample Input
10 12W........WW..WWW.....WWW....WW...WW..........WW..........W....W......W...W.W.....WW.W.W.W.....W..W.W......W...W.......W.
Sample Output
3
题意分析:
一个‘W’是一个pond,如果有其它的‘W’和这个相连(在这个的前、后、左、右、斜方向)那么认为他们一起算是一个Pond,统计一块地中一共有多少个pond。
#include<stdio.h>
#include<string.h>
int dir[8][2]={{0,1},{0,-1},{1,0},{-1,0},{1,1},{1,-1},{-1,-1},{-1,1}};
int row,column;
char a[110][110];
int visit[110][110];
void dfs(int x,int y)//写xy可以,但是下边不能用x=x+dir[i][0],y=y+dir[i][1],,因为由题x。y代表坐标在进行递归的时候坐标改变,但是递归完成后坐标还应该是从x,y开始变。例如:初始x,y为(1,1)在递归完成后再从(1,2)开始查找。如果按题中写递归完成后x,y不是这样的。。。
可以将定义改一下
void dfs(int t,int j)
{
if(visit[x][y]!=0 || a[x][y]=='.')
return ;
visit[x][y]=1;
for(int i=0;i<8;i++)
{
x=x+dir[i][0];
y=y+dir[i][1];//需要判断x,y是否越界,如果有越界是无法完成执行的、、、可以改成 int x=t+dir【i】【0】;int y=j+dir【i】【1】;dfs(x,y);从初始 t,j进行变化得到的坐标
if ( x>=1 && x<=row && y>=1 && y<=column)
dfs(x,y);
}
}
int main()
{
while(scanf("%d%d",&row,&column)!=EOF)
{
memset(visit,0,sizeof(visit));
int i,j,sum=0;
for(i=1;i<=row;i++)
{
scanf("%s",a[i]+1);
}
int x,y;
for(x=1;x<=row;x++)
{
for(y=1;y<=column;y++)
{
if(a[x][y]=='W' && visit[x][y]==0)
{
dfs(x,y);
sum++;
}
}
}
printf("%d\n",sum);
}
return 0;
}
- The number of ponds——DFS
- hdu3006——The Number of set
- sdut2623——The number of steps
- hdu 3006 The Number of set 状压dfs
- DFS---Number of Islands
- HDU 5438 Ponds(dfs)——2015 ACM/ICPC Asia Regional Changchun Online
- HDU5438(2015长春网赛)——Ponds(拓扑排序+dfs)
- hdu 5438 Ponds DFS
- 删边+DFS hdu5438 Ponds
- HDU 5437 Ponds(DFS)
- hdu 1010——Tempter of the Bone(DFS)
- hdu1010——Tempter of the Bone(DFS+剪枝)
- ACM-DFS之Tempter of the Bone——hdu1010
- 【hdu1010】Tempter of the Bone——dfs
- HDU 1010 —— Tempter of the Bone DFS+剪枝
- hdu1010 Tempter of the Bone —— dfs+奇偶性剪枝
- DFS—POJ3083-Children of the Candy Corn
- 13年山东省赛——The number of steps
- Maven Profile的使用
- awk命令详解
- C# MVC 自定义分页Helper扩展
- 十步完全理解SQL
- 抽空把大学上了
- The number of ponds——DFS
- 存储过程与触发器的应用
- iOS 高级开发技巧 1.熟悉Objective-C (三) 多用字面量语法
- Node crypto crc32 加密
- NAT工作原理深入学习NAT工作原理 我们单位中的电脑很多,组成了一个局域网,网络中只有一个电脑和外网(Internet)相连,当然有一个外网地址,但仅仅一个。我以前一直不明白,我们局域网的电脑均能上
- mfc操作浏览器(1)——执行超链接按钮
- 对一个长度为100000条指令的程序进行集成测试期间记录如下面的数据: (A)7月1日集成测试开始没有发现错误 (B)8月2日总共改正了100个错误,此时MTTF=0.4H (C)9月1日:总共改正3
- CRM助零售行业客户关系经营
- poj1151 Atlantis(hh版 扫描线)