zoukankan      html  css  js  c++  java
  • HDU 1241 Oil Deposits【DFS】

    解题思路:第一道DFS的题目---

    参看了紫书和网上的题解--

    在找到一块油田@的时候,往它的八个方向找,直到在能找到的范围内没有油田结束这次搜索 可以模拟一次DFS,比如说样例 在i=0,j=1时,发现第一块油田,对它DFS,这样经过一次DFS,所有的油田都被找出来了,被记0

    Oil Deposits

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

    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<iostream>  
    #include<cstdio>  
    #include<cstring>  
    #include<algorithm>  
    using namespace std;
    char map[1000][1000];
    int n,m;
    void dfs(int i,int j)
    {
    	if(map[i][j]!='@'||i<0||i>=m||j<0||j>=n)
    	return;
    	else //往八个方向搜索 
    	{
    		map[i][j]='0';
    		dfs(i-1,j-1);
    		dfs(i-1,j);
    		dfs(i-1,j+1);
    		dfs(i,j-1);
    		dfs(i,j+1);
    		dfs(i+1,j-1);
    		dfs(i+1,j);
    		dfs(i+1,j+1);
    	}
    }
    
    int main()
    {
    	int i,j;
    	while(scanf("%d %d",&m,&n)!=EOF&&n&&m)
    	{
    		memset(map,0,sizeof(map));
    		int ans=0;
    		for(i=0;i<m;i++)
    		scanf("%s",map[i]);
    		
    		for(i=0;i<m;i++)
    		{
    			for(j=0;j<n;j++)
    				if(map[i][j]=='@')
    				{
    				dfs(i,j);
    				ans++;
    				}
    		}
    		printf("%d
    ",ans);	
    	}
    }
    

      

  • 相关阅读:
    css知识小结(更新中)
    vim的简易操作
    shell语言学习(更新中)
    An Introduction to C & GUI Programming -----Simon Long 学习笔记 1
    fread,fwrite(二)
    fread,fwrite(一)
    printf 打印颜色
    容斥原理及证明
    字典的认识和使用 day05
    列表和元祖的使用 day 04
  • 原文地址:https://www.cnblogs.com/wuyuewoniu/p/4264173.html
Copyright © 2011-2022 走看看