Oil Deposits

传送门

 

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

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

 

题解:

题意:把一片土地分成若干个网格,网格有石油用'@'表示,如果一个有石油的网格在八个方向上有与之相邻的石油网格,那它们就同属于一个石油矿,给你一片土地,问你有多少个不同的石油矿。

思路:遍历每一个网格,如果这个网格有石油,ans++,然后dfs搜索它的八个方向上有无石油网格,有的话标记,然后继续向八个方向上搜...

#include <iostream>
#include <algorithm>
#include <cstring>
#include <map>

using namespace std;

char a[110][110];
int dx[8]={1,-1,0,0,1,1,-1,-1},dy[8]={0,0,-1,1,-1,1,-1,1};

void dfs(int x,int y)
{
	a[x][y]='*';
	for(int i=0;i<8;i++)
	{	
		int xx=x+dx[i],yy=y+dy[i];
		if(a[xx][yy]=='@') dfs(xx,yy);
	}
}

int main()
{	
	int m,n;
	while(cin>>m>>n&&(m||n)){ 
	memset(a,'*',sizeof a); 
	for(int i=0;i<m;i++)
		scanf("%s",a[i]);
	int sum=0;
	for(int i=0;i<m;i++)
		for(int j=0;j<n;j++)
		{
			if(a[i][j]=='@')
			{
				sum++;
				dfs(i,j);
			}
		}
	printf("%d\n",sum);} 
			
	return 0;
}

附:在写这个程序控制输入时,我把把油田的每一部分当成一个字符串,想着以矩阵的形式输入一个二维数组,但是却出错了,而且把矩阵输出发现矩阵输出也出错了。为了搞清错误,我尝试最简单的矩阵输入,即数字矩阵,好吧,又错误了,这么简单的程序,很自闭了。后来发现原来数字矩阵输入的时候,每个数字之间我没有加空格,导致两个数字一结合形成了一个更大的数字。进而我发现用scanf控制字符输入的时候会出错,但cin却不会,思考发现,scanf输入字符时,好像受了空格、换行的影响,百度后知道特殊格式%c利用scanf输入时,会读取任何字符,包括换行和空格。那怎么样才能利用scanf输入若干字符,而不受换行影响呢?就本题而言,可以利用%s,输入一个字符串,赋给二维数组即可。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值