码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 1241 BFS入门。。做了一个晚上,自己总算入门了。

时间:2014-12-08 23:08:12      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:acm算法   amp   c   math.h   printf   

下面上题目。

Oil Deposits
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 13340    Accepted Submission(s): 7671


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
 

Source
Mid-Central USA 1997

题目的大概意思就是给你一幅地图,让你去判断相邻,对角的油田有几个,注意,@代表油田。博主表示作为一个新手,遇到这种题目真心无力,终于今天研究了一个晚上之后,总算初窥门径,故写下这篇文章和一起学习ACM的分享。。下面是代码,附带详细的解析。

#include <stdio.h>
#include <string.h>
#define wbxxx 200   // 自己定义的。
int n,m;
char map[wbxxx][wbxxx];  //地图
int dir[8][2]={{1,1},{1,-1},{1,0},{0,1},{0,-1},{-1,0},{-1,1},{-1,-1}};  //  表示方向。向周围八个方向延伸。
int flag[wbxxx][wbxxx];   //  用这个来表示是否被访问过。
void BFS(int i,int j)   //搜索地图的i,j;
{
	int dx,dy,x;   
	   flag[i][j]=1;    //  如果被访问过,则标记一。
	   for(x=0;x<8;x++)
	   {
		   dx=i+dir[x][0];   //      搜索的方向
		   dy=j+dir[x][1];   //       搜索的方向
		   if(map[dx][dy]!='@')  //     或者访问的不是'@'则继续。
			   continue;
		   if(flag[dx][dy])   //    如果已经访问过,则继续。
			   continue;
		   BFS(dx,dy);   //   继续搜索。
	   } 
}
int main()
{
	int i,j,m,n,sum;
	while(scanf("%d%d",&n,&m)&&n!=0&&m!=0)   //  注意m和n 不能为零。。。
	{
		sum=0;   //   记得把sum赋值。
		for(i=0;i<n;i++)
			scanf("%s",map[i]);  //    输入地图
		for(i=0;i<n;i++)     //对整张地图开始搜索,对应上面的i,j;
			for(j=0;j<m;j++)     //      对整张地图开始搜索,对应上面的i,j;
			{
				if(map[i][j]=='@' &&flag[i][j]!=1)  //   如果地图经过油田,并且没有被访问过(既flag不等于1)
				{
					BFS(i,j); //继续往旁边搜索,直到没有油田,满足了对角相邻算一个。
					sum++;    // 增加一个。
	                   
				} 
			}
			printf("%d\n",sum);  //  输出
			memset(flag,0,sizeof(flag));   //  每次搜索时,记得初始化。
			memset(map,0,sizeof(map));  //每次搜索时,记得初始化。
	}
	return 0;
}


  希望初学BFS的看完有收获,博主也是纯纯的新手。以上的代码花了一个晚上研究才弄懂。。勉!!!

HDU 1241 BFS入门。。做了一个晚上,自己总算入门了。

标签:acm算法   amp   c   math.h   printf   

原文地址:http://blog.csdn.net/sky_miange/article/details/41810273

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!