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);	
    	}
    }
    

      

  • 相关阅读:
    桥接模式
    单例模式
    SpringAOP aspectJ ProceedingJoinPoint 获取当前方法
    springMVC实现文件下载
    JAVA的变量初始化类成员变量和局部变量区别
    JAVA枚举类型的应用
    linux下svn命令大全
    转:shell脚本的一些注意事项
    转: linux下不同服务器间数据传输(rcp,scp,rsync,ftp,sftp,lftp,wget,curl)
    TCP三次握手/四次挥手详解
  • 原文地址:https://www.cnblogs.com/wuyuewoniu/p/4264173.html
Copyright © 2011-2022 走看看