HDU-1241Oil Deposits

Description

GeoSurvComp地质调查公司负责探测地下石油储藏。 GeoSurvComp现在在一块矩形区域探测石油,并把这个大区域分成了很多小块。他们通过专业设备,来分析每个小块中是否蕴藏石油。如果这些蕴藏石油 的小方格相邻,那么他们被认为是同一油藏的一部分。在这块矩形区域,可能有很多油藏。你的任务是确定有多少不同的油藏。

Input

输入可能有多个矩形区域(即可能有多组测试)。每个矩形区域的起始行包含m和n,表示行和列的数量,1<=n,m<=100,如果m =0表示输入的结束,接下来是n行,每行m个字符。每个字符对应一个小方格,并且要么是‘*‘,代表没有油,要么是‘@‘,表示有油。

Output

对于每一个矩形区域,输出油藏的数量。两个小方格是相邻的,当且仅当他们水平或者垂直或者对角线相邻(即8个方向)。

Sample Input

1 1

*

3 5

*@*@*

**@**

*@*@*

1 8

@@****@*

5 5

****@

*@@*@

*@**@

@@@*@

@@**@

0 0

Sample Output

0

1

2

2


思路:

基础的经典的dfs题目,最近几天集中练习一下搜索的题目

我觉得理解搜索题目的关键在于搞懂每道题目核心的那个dfs或者bfs函数的“含义”是什么

比如说在这道题目中的dfs的含义就是把每一个遇到的@点周边所有的和他同属一个集合的点都给消去@标记,以顺应main函数中对整个图的遍历

在理解了这点之后,思路就很明朗了。

有一个小地方第一次出错了要注意一下,dir数组在本题中是8*2即“四面八方”,而不是以往的4*2,所谓的“上下左右”——这里的4或者8可以理解成方向的个数,然后第二维的2可以理解成每个方向的坐标变动情况。


#include <iostream>
#include <cstring>
using namespace std;

int m,n;
char G[107][107];
int dir[8][2] = {{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}};

void dfs(int x,int y)
{//dfs函数的抽象含义是:“标记”,即把和(x,y)同属一组的所有点都给标记出来
	G[x][y] = ‘*‘;
	for(int i = 0;i < 8;i++)
	{
		int dx = x+dir[i][0];
		int dy = y+dir[i][1];
		if(G[dx][dy]==‘@‘ && dx>=1 && dx<=m && dy>=1 && dy<=n)
			dfs(dx,dy);
	}
}

int main()
{
	while(cin>>m>>n)
	{
		if(m == 0) break;
		int ans = 0;
		for(int i = 1;i <= m;i++)
			cin>>G[i]+1;
		for(int i = 1;i <= m;i++)
			for(int j = 1;j <= n;j++)
			{
				if(G[i][j] == ‘@‘) {
					dfs(i,j);
					ans++;
				}
			}
		cout<<ans<<endl;
	}
	return 0;
}
时间: 2024-08-11 05:44:13

HDU-1241Oil Deposits的相关文章

hdu 1241Oil Deposits(BFS)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1241 Oil Deposits Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 13137    Accepted Submission(s): 7611 Problem Description The GeoSurvComp geologi

HDU 1241Oil Deposits (DFS)

Problem 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 pl

hdu 1241Oil Deposits(dfs模板)

题目链接—— http://acm.hdu.edu.cn/showproblem.php?pid=1241 首先给出一个n*m的字符矩阵,‘*’表示空地,‘@’表示油井.问在这个矩阵中有多少组油井区? 每个点周围的8个点都可以与之相连. 从左上角的点开始向后枚举然后dfs搜索就可以了.记得记忆化. 废话说完,上代码—— 1 #include <cstdio> 2 #include <cmath> 3 #include <cstring> 4 #include <a

poj - 2386 Lake Counting &amp;&amp; hdoj -1241Oil Deposits (简单dfs)

http://poj.org/problem?id=2386 http://acm.hdu.edu.cn/showproblem.php?pid=1241 求有多少个连通子图.复杂度都是O(n*m). 1 #include <cstdio> 2 3 char filed[110][110]; 4 int n,m; 5 void dfs(int x,int y) 6 { 7 for(int i=-1;i<=1;i++) 8 for(int j=-1;j<=1;j++) //循环遍历8

杭电1241--Oil Deposits(Dfs)

Oil Deposits Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 17887    Accepted Submission(s): 10298 Problem Description The GeoSurvComp geologic survey company is responsible for detecting under

hdu Oil Deposits

因为这道题中给的数据比较小,所以可以直接用枚举的方式进行dfs搜索,每出现一个‘@’,就进行一次dfs的搜索,将所有相邻的‘@’全部变成‘*’. #include"iostream" #include"stdio.h" #include"algorithm" #include"string.h" #include"cmath" #include"queue" #define mx 105

HDU 1241 Oil Deposits(石油储藏)

p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-size: 10.5000pt } h1 { margin-top: 5.0000pt; margin-bottom: 5.0000pt; text-align: center; font-family: 宋体; color: rgb(26,92,200); font-weight: bold; fo

HDU 1241 Oil Deposits --- 入门DFS

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1241 /* HDU 1241 Oil Deposits --- 入门DFS */ #include <cstdio> int m, n; //n行m列 char mapp[105][105]; /* 将和i,j同处于一个连通块的字符标记出来 */ void dfs(int i, int j){ if (i < 0 || j < 0 || i >= n || j >= m

DFS(连通块) HDU 1241 Oil Deposits

题目传送门 1 /* 2 DFS:油田问题,一道经典的DFS求连通块.当初的难题,现在看上去不过如此啊 3 */ 4 /************************************************ 5 Author :Running_Time 6 Created Time :2015-8-4 10:11:11 7 File Name :HDOJ_1241.cpp 8 ************************************************/ 9 10

hdu 1241 Oil Deposits (一次dfs搞定有某有)

1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 #include<algorithm> 5 using namespace std; 6 char map[105][105]; 7 8 int dir[8][2]={0, 1, 1, 0, -1, 0, 0, -1, 1, 1, 1, -1, -1, 1, -1, -1}; 9 int n, m; 10 int ans; 11 12