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. Rsync 实现远程同步

    介绍 rsync命令是一个远程数据同步工具,可通过LAN/WAN快速同步多台主机间的文件.rsync使用所谓的“rsync算法”来使本地和远程两个主机之间的文件达到同步,这个算法只传送两个文件的不同部 ...

  2. the little schemer 笔记(6)

    第六章 Shadows 1 是算术表达式吗 是 3 是算术表达式吗 是的 1+3 是算术表达式吗 是的 1+3×4 是算术表达式吗 当然是 cookie 是算术表达式吗 是啊,你需要来一块吗 e那么 ...

  3. multiprocessing多进程模块

    1 基本理解 python不支持真多线程,无法在单进程中调用多核cpu.对于cpu密集型任务,可以使用多进程.python会调用OS原生多进程,运行在多核上,以此提高运行速度. 2 基本实现 impo ...

  4. AtCoder Grand Contest 015 C - Nuske vs Phantom Thnook

    题目传送门:https://agc015.contest.atcoder.jp/tasks/agc015_c 题目大意: 现有一个\(N×M\)的矩阵\(S\),若\(S_{i,j}=1\),则该处为 ...

  5. Suricata的命令行解释

    见官网 https://suricata.readthedocs.io/en/latest/command-line-options.html root@SELKS:~# suricata Suric ...

  6. 【转】grep 用法详解

    有时会使用到,但老忘,转到博客以便学习收藏 转自http://blog.csdn.net/tenfyguo/article/details/6387786 首先要记住的是: 正则表达式与通配符不一样, ...

  7. java只http改成https访问

    目录 生成keystore文件 修改tomcat中的server.xml文件 配置浏览器 生成keystore文件: 1.在tomcat的bin 目录下输入命令:keytool -genkeypair ...

  8. PHP设计模式 观察者模式(Observer)

    定义 当一个对象状态发生改变时,依赖它的对象全部会收到通知,并自动更新. 模式要点 Event:事件 Trigger() 触发新的事件 abstract EventGenerator 事件产生者 Fu ...

  9. java实现斐波那契的两种方法

    package com.ywx.count; /** * 斐波那契数列(地推方式要比递归方式的效率要高) * @author Vashon(yangwenxue) * date:20150320 */ ...

  10. casting in C++

    这是2013年写的一篇旧文,放在gegahost.net上面 http://raison.gegahost.net/?p=39 February 20, 2013 casting in C++ Fil ...