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. 
 

Input

The input file contains one or more grids. Each grid begins with a line containing m and n, the number of rows and columns in the grid, separated by a single space. If m = 0 it signals the end of the input; otherwise 1 <= m <= 100 and 1 <= n <= 100. Following this are m lines of n characters each (not counting the end-of-line characters). Each character corresponds to one plot, and is either `*', representing the absence of oil, or `@', representing an oil pocket. 
 

Output

For each grid, output the number of distinct oil deposits. Two different pockets are part of the same oil deposit if they are adjacent horizontally, vertically, or diagonally. An oil deposit will not contain more than 100 pockets. 
 

Sample Input

1 1
*
3 5
*@*@*
**@**
*@*@*
1 8
@@****@*
5 5 ****@
*@@*@
*@**@
@@@*@
@@**@
0 0
 

Sample Output

0
1
2
2
定义个数组,让@的出现的位置为1,*出现的位置为0;从@出现的位置开始找,找到一个1,把它变成0,具体看测试结果。
 #include<cstdio>
#include<string.h>
int i,j,m,n,map[][];
char cha[],ans;
int dx[]={-,,,,,,-,-};
int dy[]={-,-,-,,,,,};
void f(int x,int y)
{
int i,nx,ny;
for(i = ; i < ; i++)
{
nx=x+dx[i];
ny=y+dy[i];
if(nx>= && ny>= && nx<m && ny<n && map[nx][ny]==)
{
map[nx][ny]=;
f(nx,ny);
}
}
}
int main()
{
while(scanf("%d %d",&m,&n)&&m&&n)
{
memset(map,,sizeof(map));
ans=;
for(i = ; i < m ; i++)
{
scanf("%s",&cha);
for(j = ; j < n ; j++)
{
if(cha[j] == '@')
{
map[i][j]=;
}
}
}
/* for( i = 0 ; i < m ; i++)
{
for(j =0 ; j < n ; j++)
{
printf("%d ",map[i][j]);
if(j == n-1)
{
printf("\n");
}
}
}*/
for(i = ; i < m ; i++)
{
for(j = ; j < n ; j++)
{
if(map[i][j] == )
{
map[i][j]==;
f(i,j);
ans++;
}
}
}
printf("%d\n",ans);
}
}

bfs解法

 #include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
int m,n,map[][],ans,i,j;
char str[];
int dx[]={-,,,,,,-,-};
int dy[]={-,-,-,,,,,};
struct stu
{
int x,y;
}st;
void bfs(int xx,int yy)
{
stu next;
int i;
st.x=xx;
st.y=yy;
queue<stu>que;
que.push(st);
while(!que.empty())
{
st=que.front();
que.pop();
for(i = ; i < ; i++)
{
int nx,ny;
nx=st.x+dx[i];
ny=st.y+dy[i];
if(nx>= && ny>= && nx<m && ny<n && map[nx][ny]==)
{
map[nx][ny]=;
next.x=nx;
next.y=ny;
que.push(next);
}
} }
}
int main()
{
while(scanf("%d %d",&m,&n) && m && n)
{
memset(map,,sizeof(map));
ans=;
for(i = ; i < m ; i++)
{
scanf("%s",&str);
for(j = ; j < n ; j++)
{
if(str[j] == '@')
{
map[i][j]=;
}
}
}
for(i = ; i < m ;i++)
{
for(j = ;j < n ; j++)
{
if(map[i][j] == )
{
map[i][j]=;
bfs(i,j);
ans++;
}
}
}
printf("%d\n",ans);
}
}

杭电 1241 Oil Deposits (很好的dfs)的更多相关文章

  1. 杭电1241 Oil Deposits

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission ...

  2. HDU 1241 Oil Deposits(石油储藏)

    HDU 1241 Oil Deposits(石油储藏) 00 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)   Probl ...

  3. HDU 1241 Oil Deposits --- 入门DFS

    HDU 1241 题目大意:给定一块油田,求其连通块的数目.上下左右斜对角相邻的@属于同一个连通块. 解题思路:对每一个@进行dfs遍历并标记访问状态,一次dfs可以访问一个连通块,最后统计数量. / ...

  4. hdu 1241 Oil Deposits(DFS求连通块)

    HDU 1241  Oil Deposits L -DFS Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & ...

  5. HDOJ(HDU).1241 Oil Deposits(DFS)

    HDOJ(HDU).1241 Oil Deposits(DFS) [从零开始DFS(5)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架 ...

  6. DFS(连通块) HDU 1241 Oil Deposits

    题目传送门 /* DFS:油田问题,一道经典的DFS求连通块.当初的难题,现在看上去不过如此啊 */ /************************************************ ...

  7. HDOJ/HDU 1241 Oil Deposits(经典DFS)

    Problem Description The GeoSurvComp geologic survey company is responsible for detecting underground ...

  8. hdu 1241:Oil Deposits(DFS)

    Oil Deposits Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total ...

  9. HDU 1241 Oil Deposits DFS(深度优先搜索) 和 BFS(广度优先搜索)

    Oil Deposits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...

随机推荐

  1. WOW.js 动画使用

    有的页面在向下滚动的时候,有些元素会产生细小的动画效果.虽然动画比较小,但却能吸引你的注意.比如刚刚发布的 iPhone 6 的页面(查看).如果你希望你的页面也更加有趣,那么你可以试试 WOW.js ...

  2. 用JS改变页面中b标签的样式啊 样式的等

    用JS改变页面中b标签的样式啊 样式的等    ,实际上是在标签内加上样式 ,用媒体查询的话 ,不能生效 <!DOCTYPE html> <html lang="en&qu ...

  3. 洛谷 P1072 Hankson 的趣味题 || 打质数表的分解质因数

    方法就是枚举,根据b0和b1可以大大减小枚举范围,方法类似这个http://blog.csdn.net/hehe_54321/article/details/76021615 将b0和b1都分解质因数 ...

  4. SpringMVC和MyBatis的整合

    这里我们需要用到一个关键的jar包——Spring-MyBatis,它会帮你将MyBatis代码无缝地整合到Spring中.具体可以参考http://www.mybatis.org/spring/zh ...

  5. 【前端】jq弹出一个透明小提示窗,然后逐渐消失

      function show_main(content) { var showWindow = '<div id="show_main" style="borde ...

  6. CF758C Unfair Poll

    题意: On the Literature lesson Sergei noticed an awful injustice, it seems that some students are aske ...

  7. CF750D New Year and Fireworks

    题意: 放烟花. 一个烟花从某一起点开始,沿着当前方向移动指定数量的格子之后爆炸分成两部分,分别沿着当前方向的左上和右上方向移动.而每一部分再沿着当前方向移动指定数量的格子之后爆炸分成两部分.如此递归 ...

  8. Android图片压缩上传(二)

    之前有用到libjpeg,还是有一定的局限性,最近用了一个新的方式,效果还是挺不错,随着作者的版本更新,Bug也随之变少,目前项目中运用已上线. 1.之前的方式Android图片压缩,不失真,上线项目 ...

  9. 第一章 熟悉Objective -C 编写高质量iOS与OS X代码的52 个有效方法

    第一章 熟悉Objective -C   编写高质量iOS与OS  X代码的52 个有效方法   第一条: 了解Objective-C 语言的起源 关键区别在于 :使用消息结构的语言,其运行时所应执行 ...

  10. How `delete’ works ?

    这是2013年写的一篇旧文,放在gegahost.net上面 http://raison.gegahost.net/?p=21 February 16, 2013 How `delete’ works ...