ACM集训第二天

两天的集训下来在慢慢的摸索过程中逐渐有了一点领会。

先是讲的dfs深度优先搜索法,然后是bfs广度优先搜索法,但是在代码书写的过程却总是出现一些问题。

DFS的主要思想是利用递归,每次递归都会带入下一个节点数据。而BFS则是使用队列或是优先队列进行广度优先的搜索方式。

以下列出一道杭电的题目。

Oil Deposits

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 13991    Accepted Submission(s): 8044



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
 

Recommend
Eddy   |   We have carefully selected several similar problems for you:   1016  1010  1312  1242  1240 
 
这道题其实是一道简化了的紫书上的例题,这道题就是用DFS的一道典型的题目。而在处理dfs的时候一定要注意它的return而且一定先标记,就像用BFS一样,一定是先做好修改在进行push。

下面附上十分拙劣的源码:

#include<cstdio>
#define maxn1 105
#define maxn2 105

using namespace std;

char map[maxn1][maxn1];
int oil[maxn1][maxn1];
int mov[8][2] = {-1,0,1,0,0,-1,0,1,-1,-1,-1,1,1,-1,1,1};
int m,n;
void dfs(int x,int y){
			for(int i = 0;i < 8;i++){
				if(x+mov[i][0] >= 0&& x+mov[i][0] < n&&y + mov[i][1]>=0 && y + mov[i][1] < m){
					if(oil[y+mov[i][1]][x+mov[i][0]]){
						oil[y+mov[i][1]][x+mov[i][0]] = 0;
						dfs(x+mov[i][0],y+mov[i][1]);
					}else
						continue;
				}else
					continue;
			}
		}
int main(){
	while(scanf("%d %d",&m,&n) && m && n){
		for(int i = 0;i < m;i++){
			for(int j = 0;j < n;j++){
				scanf(" %c",&map[i][j]);
				if(map[i][j] == '@')
					oil[i][j] = 1;
				else if(map[i][j] == '*')
					oil[i][j] = 0;
			}
		}
		int cnt = 0;
		for(int i = 0;i < m;i++){
			for(int j = 0;j < n;j++){
				if(oil[i][j]){
					cnt += 1;
					dfs(j,i);
				}
			}
		}
		printf("%d\n",cnt);
	}
	return	0;
}


1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值