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








  • 相关阅读:
    mvc edmx 的config文件
    Openwrt安装软件的方法
    如何用JQuery实现单元格 循环变背景色
    Vue的生命周期
    hmtl弹出框样式
    在星巴克买咖啡思考技术团队的管理
    RealTimePerformanceDemoView
    《SAAS创业指南》拆书笔记——产品打磨和商业模式初步验证
    基于日志的回放对比系统设计
    异常测试-中间件故障演练
  • 原文地址:https://www.cnblogs.com/bhlsheji/p/4040453.html
Copyright © 2020-2023  润新知