宝岛探险2
2015-08-13 20:44
295 查看
#include<iostream> using namespace std; int book[51][51]={0}; int a[51][51]; int sum; int n,m; void dfs(int x,int y,int color) { int tx,ty; int k; //定义一个方向的数组 int next[4][2]={{0,1},{1,0},{0,-1},{-1,0}}; //枚举4个方向 a[x][y]=color; for(k=0;k<4;k++) { //计算下一步的坐标 tx=x+next[k][0]; ty=y+next[k][1]; //判断是否越界 if(tx<1||tx>n||ty<1||ty>m) { continue; } //判断是否是陆地 if(a[tx][ty]>0&&book[tx][ty]==0) { sum++; book[tx][ty]=1;//标记这个点已经走过 dfs(tx,ty,color);//开始尝试下一个点 } } return ; } int main() { int i,j; int num=0; cin>>n>>m; //读入地图 for(i=1;i<=n;i++) { for(j=1;j<=m;j++) { cin>>a[i][j]; } } //对每一个大于0的点尝试进行dfs染色 for(i=1;i<=n;i++) { for(j=1;j<=m;j++) { if(a[i][j]>0) { num--; book[i][j]=1; dfs(i,j,num); } } } //输出染色后的地图 for(i=1;i<=n;i++) { for(j=1;j<=m;j++) { cout<<a[i][j]<<" "; } cout<<endl; } cout<<"小岛个数:"<<-num<<endl; return 0; } /* 10 10 1 2 1 0 0 0 0 0 2 3 3 0 2 0 1 2 1 0 1 2 4 0 1 0 1 2 3 2 0 1 3 2 0 0 0 1 2 4 0 0 0 0 0 0 0 0 1 5 3 0 0 1 2 1 0 1 5 4 3 0 0 1 2 3 1 3 6 2 1 0 0 0 3 4 8 9 7 5 0 0 0 0 0 3 7 8 6 0 1 2 0 0 0 0 0 0 0 0 1 0 */
相关文章推荐
- 信息系统权限设计二
- 程序员必须掌握的十种算法---快速排序算法
- 智能家居为什么离不开ZigBee协议
- Android-应用开发-多媒体编程(九)
- hud
- vmax Degraded lun
- UI11_Block传值
- 数据库优化1(逻辑结构)
- hdu 5375 gray code
- 开始写博客
- ZOJ 1081 Points Within
- leetcode Balanced Binary Tree 平衡二叉树判定
- 数组操作
- WPf中多线程改UI
- [leetcode] two sum
- HDU 5099 Comparison of Android versions【字符串+模拟】
- http://zzgthk.iteye.com/blog/1064434
- cocos2d-x由Jni实现Java与C++打电话给对方
- Android-应用开发-广播和服务(八)
- cell的自适应高度