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. Hdu 5446 Unknown Treasure (2015 ACM/ICPC Asia Regional Changchun Online Lucas定理 + 中国剩余定理)

    题目链接: Hdu 5446 Unknown Treasure 题目描述: 就是有n个苹果,要选出来m个,问有多少种选法?还有k个素数,p1,p2,p3,...pk,结果对lcm(p1,p2,p3.. ...

  2. HDU6446(树上、排列的贡献计算)

    关键点在于:全排列中,任意两点u.v相邻的次数一定是(n - 1)! * 2次,即一个常数(可以由高中数学知识计算,将这两个点捏一起然后全排列然后乘二:或者用n! / C(2, n)). 这之后就好算 ...

  3. UVa 11437 (梅涅劳斯定理) Triangle Fun

    题意: 给出三角形ABC顶点的坐标,DEF分别是三边的三等分点.求交点所形成的三角形PQR的面积. 分析: 根据梅涅劳斯定理,我们得到: ,解得 另外还有:,解得 所以AR : RP : PD = 3 ...

  4. 线段树+扫描线 HDOJ 5091 Beam Cannon(大炮)

    题目链接 题意: 给出若干个点的坐标,用一个W*H的矩形去覆盖,问最多能覆盖几个点. 思路: 这是2014上海全国邀请赛的题目,以前写过,重新学习扫描线.首先把所有点移到第一象限([0, 40000] ...

  5. c#学习系列之装箱拆箱

    1.      装箱和拆箱是一个抽象的概念 2.      装箱是将值类型转换为引用类型 :拆箱是将引用类型转换为值类型       利用装箱和拆箱功能,可通过允许值类型的任何值与Object 类型的 ...

  6. AO-XXXX

    一 AO4419:应用于开关应用或PWM应用的场效应管.

  7. MonoBehaviour生命周期

    MonoBehaviour生命周期 上图中重要的信息点很多,需要特别注意的是所有脚本的Awake方法都执行完才会执行Start,但是如果在Awake 中开启了一个协程这个协程中每一帧执行一些操作然后等 ...

  8. poj2112Optimal Milking(二分+最大流)

    链接 floyd求出牛到机器的最短距离,二分距离,小于当前距离的边容量设为1,求出满容量下的最短距离. EK算法 #include <iostream> #include<cstdi ...

  9. 公有云大脑——核心IDC简影

    出差刚到家,公司最近接了一个矿场转建公有云平台的项目. 前期200台服务器作为公有云基础. 我主要负责总体网络规划.计费数据库集群设计.ceph集群自动部署.容器化设计.硬件及系统调试优化等等! 由于 ...

  10. TigerGraph REST++API

    简介 - 什么是REST ++? TigerGraph TM 系统使用着名的REpresentational State Transfer(REST)架构来管理与TigerGraph核心组件,图形处理 ...