HDu1241 DFS搜索
2013-11-03 16:06
357 查看
#include<iostream> #include<cstring> using namespace std; int a[105][105]; int d[8][2]={0,1,0,-1,1,0,-1,0,-1,-1,-1,1,1,-1,1,1}; //八个方向!方向向量。 void dfs(int x,int y) { a[x][y]=-1; //相连的无效 for(int i=0;i<8;i++) { if(a[x+d[i][0]][y+d[i][1]]==1) dfs(x+d[i][0],y+d[i][1]); //继续搜索 } } int main() { int n,m; while(cin>>n>>m&&(n||m)) { memset(a,0,sizeof(a)); //拓广边界 int i,j; char x; for(i=1;i<=n;i++) for(j=1;j<=m;j++) { cin>>x; if(x=='@')a[i][j]=1; } int count=0; for(i=1;i<=n;i++) for(j=1;j<=m;j++) { if(a[i][j]==1) { count++; dfs(i,j); } } cout<<count<<endl; } return 0; }
相关文章推荐
- 在Android Studio 中加入jar 和.so 文件
- Hibernate——(3)主键生成方式
- Hibernate——(3)主键生成方式
- HDU3067:小t的游戏
- 庞果网之字符串消除
- 中国证书CHINASSL正式启用中国证书.com
- 了解Activity的Intent用法
- linux shell脚本编程技巧介绍(一)
- LeetCode -- Single Number 代码分析
- C++编程——微生物增殖
- [Unity3D]AssetBundles的使用
- Java设计模式—单态模式(Singleton)
- Linux Shell笔记之准备篇
- [Unity3D]AssetBundles的使用
- Android Studio 不自动弹起代码提示功能解决办法 do not auto popup code completion
- 装饰模式(Decorator)
- UVA 10081 Tight Words
- LeetCode 之 Best Time to Buy and Sell Stock
- Apache Cxf WebService整合Spring(1)
- Documentation_timers_timers-howto