zoukankan      html  css  js  c++  java
  • 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
    ",cnt);
    	}
    	return	0;
    }


  • 相关阅读:
    python split的用法
    大学排名爬虫
    一直在报错:ModuleNotFoundError: No module named 'bs4'.
    微信小程序部署到线上环境
    mybatis关联三张表查询对应字段名
    WPF图像裁剪控件
    git创建标签并推送到远程
    Windows7、10的热键屏蔽
    WPF使用SVG简单整理
    Visual Studio 注册码和下载
  • 原文地址:https://www.cnblogs.com/lccurious/p/5079889.html
Copyright © 2011-2022 走看看