zoukankan      html  css  js  c++  java
  • hud 1241 Oil Deposits

    Oil Deposits

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


    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版:
    #include <cstdio>
    #include <iostream>
    #include <cstring>
    #include <queue>
    using namespace std;
    char date[110][110];
    bool map[110][110];
    int n,m;
    int dfs(int a,int b)
    {
    	if(map[a][b]==0) return 0;
    	else {
    		map[a][b]=0;
    		if(a>0){
    			if(b>0) dfs(a-1,b-1);
    			if(b<n-1) {
    				dfs(a-1,b+1);
    			}
    			dfs(a-1,b);
    		}
    		if(a<m-1)
    		{
    			if(b>0) dfs(a+1,b-1);
    			if(b<n-1)dfs(a+1,b+1);
    			dfs(a+1,b);
    		}
    		if(b>0) dfs(a,b-1);
    		if(b<n-1) dfs(a,b+1);
    		return 1;
    	}
    }
    int main()
    {
    	char chare;
    	int ans;
    	//freopen("in.txt","r",stdin);
    	while (~scanf("%d%d",&m,&n))
    	{
    		//printf("%d %d
    ",m,n);
    		if(n==0&&m==0) break;
    		ans=0;
    		chare='a';
    		for (int i=0;i<m;i++) cin>>date[i];
    		for (int i=0;i<m;i++){
    			for (int j=0;j<n;j++)
    			{
    				map[i][j]=date[i][j]=='@'?true:false;
    			}//puts("");
    		}
    		for (int i=0;i<m;i++)
    		{
    			for (int j=0;j<n;j++)
    			{
    				ans+=dfs(i,j);
    			}
    		}
    		printf("%d
    ",ans);
    	}
    	return 0;
    }

    bfs版
    #include <cstdio>
    #include <iostream>
    #include <cstring>
    #include <queue>
    using namespace std;
    char map[110][110];
    int n,m;
    queue<pair<int,int> > q;
    int bfs(int a,int b)
    {
    	if(map[a][b]=='*') return 0;
    	else  {
    		q.push(make_pair(a,b));
    		while (!q.empty())
    		{
    			int x,y;
    			x=q.front().first;
    			y=q.front().second;
    			if(x>0){
    				if(y>0) if(map[x-1][y-1]=='@'){
    					map[x-1][y-1]='*';
    					q.push(make_pair(x-1,y-1));
    				}//bfs(a-1,b-1);
    				if(y<n-1) if(map[x-1][y+1]=='@')
    				{
    					map[x-1][y+1]='*';
    					q.push(make_pair(x-1,y+1));
    				}//bfs(a-1,b+1);
    				if(map[x-1][y]=='@')
    				{
    					map[x-1][y]='*';
    					q.push(make_pair(x-1,y));
    				}//bfs(a-1,b);
    			}
    			if(x<m-1)
    			{
    				if(y>0) if(map[x+1][y-1]=='@'){
    					map[x+1][y-1]='*';
    					q.push(make_pair(x+1,y-1));
    				}//bfs(a-1,b-1);
    				if(y<n-1) if(map[x+1][y+1]=='@')
    				{
    					map[x+1][y+1]='*';
    					q.push(make_pair(x+1,y+1));
    				}//bfs(a-1,b+1);
    				if(map[x+1][y]=='@')
    				{
    					map[x+1][y]='*';
    					q.push(make_pair(x+1,y));
    				}//bfs(a-1,b);
    			}
    			if(y>0) 
    				if(map[x][y-1]=='@')
    				{
    					map[x][y-1]='*';
    					q.push(make_pair(x,y-1));
    				}//bfs(a,b-1);
    			if(y<n-1) 
    				if(map[x][y+1]=='@')
    				{
    					map[x][y+1]='*';
    					q.push(make_pair(x,y+1));
    				}//bfs(a,b+1);
    				q.pop();
    		}
    		return 1;
    	}
    }
    int main()
    {
    	char chare;
    	int ans;
    	//freopen("in.txt","r",stdin);
    	while (~scanf("%d%d",&m,&n))
    	{
    		if(n==0&&m==0) break;
    		ans=0;
    		chare='a';
    		for (int i=0;i<m;i++){ cin>>map[i];}
    		for (int i=0;i<m;i++)
    		{
    			for (int j=0;j<n;j++)
    			{
    				ans+=bfs(i,j);
    			}
    		}
    		printf("%d
    ",ans);
    	}
    	return 0;
    }








  • 相关阅读:
    python开发_html_html处理
    python开发_logging_日志处理
    IIS 7 应用程序池自动回收关闭的解决方案
    C#流水号生成汇总(四)
    通过内存盘提高MSMQ的消息吞吐能力
    c#分页工具类,完美实现List分页
    IIS 未能从程序集“System.ServiceModel, Version=3.0.0.0, Culture=neutral,
    win7电脑遇到端口被占用的情况该如何查看并将其关闭
    如何创建内存盘
    IP地址便捷修改器 V3.5 绿色版
  • 原文地址:https://www.cnblogs.com/bhlsheji/p/4040453.html
Copyright © 2011-2022 走看看