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. bryce1010专题训练——划分树

    1.求区间第K大 HDU2665 Kth number /*划分树 查询区间第K大 */ #include<iostream> #include<stdio.h> #inclu ...

  2. 注册jdbc驱动的三种方式

    java.sql.DriverManger类简介   java的驱动管理类.管理一组 JDBC 驱动程序. javax.sql.DataSource 接口是 JDBC 2.0 API 中的新增内容,它 ...

  3. windows密码长度最小值改不了

    控制台输入gpedit.msc或者在“开始→控制面板→管理工具→本地安全策略→账户策略→密码策略→密码长度最小值”中修改不了,是灰色的,不让修改 用命令行可以修改开始-->运行-->输入& ...

  4. 用python编写的excel拆分小工具

    from datetime import date,datetime from openpyxl import Workbook from openpyxl import load_workbook ...

  5. Cannot call sendRedirect()/forward after the response has been committed的问题

    问题其实已经很明确了,说明就是不能重定向,因为已经有response了. 然后一检查,是前面已经用servlet的printWriter打印东西了. 所以,重定向前 必须先保证没有任何的输出,包括:1 ...

  6. 域名IP主动验证(一)

    功能:主动验证给定的域名.IP对是否真正的关联 思路: 1.一开始通过修改hosts文件,把待验证的域名.IP对添加到文件里,然后用wget尝试访问,再恢复hosts文件重新验证下一对 2.后来了解到 ...

  7. android开发学习 ------- git - 将代码回滚到任意版本

    不小心将一个东西错误提交到git - 远程仓库上 参考  https://www.cnblogs.com/wancy86/p/5848024.html 你的git可能关联了多个远程仓库,每个关联的代码 ...

  8. AJPFX总结private关键字

    private关键字        什么是private关键字?                它是一个修饰符,代表私有的意思,它可以修饰成员变量和成员方法 private关键字的特点?        ...

  9. 类成员的指针必须NULL化,否则是乱七八糟的东西

    class BiTree { public: BiTree(); virtual ~BiTree(); virtual void insertNode(Node * newNode); virtual ...

  10. 一个Java编写的小玩意儿---多人在线聊天工具

    这个在线聊天工具小项目使用JAVA编写,用JAVA来做图形界面本来就是出了名的低效和丑陋.不过这不是重点.写这个小项目的目的在于串一串J2SE的知识,把当时写这个项目的时候的思路梳理一下.时间有点久了 ...