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. js 合并2个结构

    var conditions = getJsonObj("conditionArea"); var plogId = { ProgId: getProgId() }; $.exte ...

  2. Arthur and Table CodeForces - 557C

    Arthur and Table CodeForces - 557C 首先,按长度排序. 长度为p的桌腿有a[p]个. 要使得长度为p的桌腿为最长,那么要按照代价从小到大砍掉sum{长度不到p的腿的数 ...

  3. fscanf

    fscanf (PHP 4 >= 4.0.1, PHP 5, PHP 7) fscanf — 从文件中格式化输入 说明 mixed fscanf ( resource $handle , str ...

  4. selenium处理的操作

  5. 关于发布WP 8.1应用信息不匹配问题的解决办法

    错误提示:   与此更新关联的程序包标识符与已上传程序包中的标识符不匹配: The package identity associated with this update doesn't match ...

  6. SSM-WebMVC(三)

    SSM-WebMVC(三) 一.Annotated Controllers ​ 应用程序控制器 handlerMethod(处理方法) ㈠方法入参 ​ (springmvc针对于在controller ...

  7. JVM初探

    ### JVM分为类的加载生命周期和gc垃圾回收两个大的方面#####首先是类的生命周期, 类的加载: --> 记载字节码 ---> 这个过程有类的加载起参与,双亲委托机制() --> ...

  8. re正则表达式讲解—初步认识

    # f = open(r"C:\Users\LENOVO\Desktop\模特.txt",'r') # 1.常规提取文档内容方法 # contacts = [] # for i i ...

  9. 洛谷 P2604 [ZJOI2010]网络扩容

    题目描述 给定一张有向图,每条边都有一个容量C和一个扩容费用W.这里扩容费用是指将容量扩大1所需的费用.求: 1. 在不扩容的情况下,1到N的最大流: 2. 将1到N的最大流增加K所需的最小扩容费用. ...

  10. SQLite – DISTINCT 关键字

    SQLite – DISTINCT关键字 使用SQLite DISTINCT关键字与SELECT语句来消除所有重复的记录和获取唯一的记录. 可能存在一种情况,当你有多个表中重复的记录. 获取这些记录, ...