Oil Deposits HDU杭电1241

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

//挺容易理解的,解释在代码里

#include<stdio.h>
int n,m;
char a[105][105];
void DFS(int x,int y)
{
	if(x>m || x<1 || y>=n || y<0)//越界
		return ;
	if(a[x][y]!='@')//不是油
		return ;

	a[x][y]='+';//这样就少执行了些,节省了点时间

	DFS(x,y+1);	//上下左右对角线
	DFS(x,y-1);
	DFS(x-1,y);
	DFS(x+1,y);
	DFS(x-1,y-1);
	DFS(x+1,y+1);
	DFS(x-1,y+1);
	DFS(x+1,y-1);
}
int main()
{
	int i,j;
	int x,y;
	while(scanf("%d%d",&m,&n),m+n)
	{
		getchar();
		for(i=1;i<=m;++i)
		{
			scanf("%s",a[i]);//用这个更方便
		}
		int cnt=0;
		for(i=1;i<=m;++i)
		{
			for(j=0;j<n;++j)
			{
				if(a[i][j]=='@')
				{
					cnt++;
					DFS(i,j);
				}
			}
		}
		printf("%d\n",cnt);

	}
	return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-16 23:12:04

Oil Deposits HDU杭电1241的相关文章

Oil Deposits(杭电oj1241)(BFS)

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

『ACM C++』HDU杭电OJ | 1415 - Jugs (灌水定理引申)

今天总算开学了,当了班长就是麻烦,明明自己没买书却要带着一波人去领书,那能怎么办呢,只能说我善人心肠哈哈哈,不过我脑子里突然浮起一个念头,大二还要不要继续当这个班委呢,既然已经体验过就可以适当放下了吧,用心在自己的研究上.晚上级会开完也就八点多了,开始打打题,今天在HDU杭电的ACM集训题看到一个奇葩的题,前来献上. 今日推荐: <全球风暴> 一部宇宙航空和地球气候片的良心佳作,后期特效建模都是特别杠杠的大片,不会让你失望的哟,我已经三刷了哈哈哈.这部片在爱奇艺有上线,有兴趣的朋友可以看看鸭.

(搜索)Oil Deposits -- hdu -- 1241

链接: http://acm.hdu.edu.cn/showproblem.php?pid=1241 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 18758    Accepted Submission(s): 10806 Problem Description The GeoSurvComp geologic survey comp

一个人的旅行 HDU杭电2066【dijkstra算法】

http://acm.hdu.edu.cn/showproblem.php?pid=2066 Problem Description 虽然草儿是个路痴(就是在杭电待了一年多,居然还会在校园里迷路的人,汗~),但是草儿仍然很喜欢旅行,因为在旅途中 会遇见很多人(白马王子,^0^),很多事,还能丰富自己的阅历,还可以看美丽的风景--草儿想去很多地方,她想要去东京铁塔看夜景,去威尼斯看电影,去阳明山上看海芋,去纽约纯粹看雪景,去巴黎喝咖啡写信,去北京探望孟姜女--眼看寒假就快到了,这么一大段时间,可不

一个人的旅行 HDU杭电2066【dijkstra算法 || SPFA】

http://acm.hdu.edu.cn/showproblem.php? pid=2066 Problem Description 尽管草儿是个路痴(就是在杭电待了一年多,竟然还会在校园里迷路的人.汗~),但是草儿仍然非常喜欢旅行,由于在旅途中 会遇见非常多人(白马王子.^0^),非常多事,还能丰富自己的阅历.还能够看漂亮的风景--草儿想去非常多地方.她想要去东京铁塔看夜景,去威尼斯看电影,去阳明山上看海芋.去纽约纯粹看雪景.去巴黎喝咖啡写信.去北京探望孟姜女--眼看寒假就快到了,这么一大段

Oil Deposits HDU - 1241 (简单bfs)(找有多少个连通块)

题目链接:https://cn.vjudge.net/problem/HDU-1241 注意:搜索八个方向 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <queue> 5 #include <stack> 6 #include <algorithm> 7 #include <cmath> 8 #include <m

畅通project续HDU杭电1874【dijkstra算法 || SPFA】

http://acm.hdu.edu.cn/showproblem.php?pid=1874 Problem Description 某省自从实行了非常多年的畅通project计划后.最终修建了非常多路.只是路多了也不好,每次要从一个城镇到还有一个城镇时,都有很多种道路方案能够选择,而某些方案要比还有一些方案行走的距离要短非常多.这让行人非常困扰. 如今,已知起点和终点,请你计算出要从起点到终点.最短须要行走多少距离. Input 本题目包括多组数据.请处理到文件结束. 每组数据第一行包括两个正

Choose the best route HDU杭电2680【dijkstra算法】

http://acm.hdu.edu.cn/showproblem.php?pid=2680 Problem Description One day , Kiki wants to visit one of her friends. As she is liable to carsickness , she wants to arrive at her friend's home as soon as possible . Now give you a map of the city's tra

六度分离 HDU杭电1869【dijkstra算法】

http://acm.hdu.edu.cn/showproblem.php?pid=1869 Problem Description 1967年,美国著名的社会学家斯坦利·米尔格兰姆提出了一个名为"小世界现象(small world phenomenon)"的著名假说,大意是说,任何2个素不相识的人中间最多只隔着6个人,即只用6个人就可以将他们联系在一起,因此他的理论也被称为"六度分离"理论(six degrees of separation).虽然米尔格兰姆的理论