码迷,mamicode.com
首页 > 编程语言 > 详细

dfs 算法

时间:2016-05-02 21:11:20      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:

Problem Description
The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits.
GeoSurvComp works with one large rectangular region of land at a time, and creates a grid that
divides the land into numerous square plots. It then analyzes each plot separately, using sensing
equipment to determine whether or not the plot contains oil. A plot containing oil is called a pocket.
If two pockets are adjacent, then they are part of the same oil deposit. Oil deposits can be quite large
and may contain numerous pockets. Your job is to determine how many different oil deposits are contained in a grid
技术分享
技术分享
技术分享技术分享

#include<cstdio> int dir[8][2]={{0,-1},{0,1},{-1,0},{1,0},{-1,-1},{1,-1},{-1,1},{1,1}}; char map[100][100]; int count; int m,n; void dfs(int x,int y) { int i=0; if(x<0||y<0||x==m||y==n) return; if(map[x][y]==*) return; else { map[x][y]=*; for(i=0;i<8;i++) dfs(x+dir[i][0],y+dir[i][1]); } } int main() { int i=0; int j=0; while(~scanf("%d%d",&m,&n)&&m) { for(i=0;i<m;i++) scanf("%s",map[i]); count=0; for(i=0;i<m;i++) for(j=0;j<n;j++) { if(map[i][j]==@) { count++; dfs(i,j); } } printf("%d\n",count); } return 0; }

 

 

 

技术分享

 

dfs 算法

标签:

原文地址:http://www.cnblogs.com/test404/p/5453070.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!