POJ 1562 Oil Deposits

Description

The GeoSurvComp geologicsurvey company is responsible for detecting underground oil deposits.GeoSurvComp works with one large rectangular region of land at a time, andcreates a grid that divides the land into numerous square plots. It thenanalyzes each plot separately, using sensing equipment to determine whether ornot the plot contains oil. A plot containing oil is called a pocket. If twopockets are adjacent, then they are part of the same oil deposit. Oil depositscan be quite large and may contain numerous pockets. Your job is to determinehow many different oil deposits are contained in a grid.

Input

The input contains one or more grids. Each grid begins with a linecontaining m and n, the number of rows and columns in the grid, separated by asingle 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 characterseach (not counting the end-of-line characters). Each character corresponds toone plot, and is either `*', representing the absence of oil, or `@',representing an oil pocket.

Output

are adjacent horizontally,vertically, or diagonally. An oil deposit will not contain more than 100pockets.

Sample Input

1 1

*

3 5

*@*@*

**@**

*@*@*

1 8

@@****@*

5 5

****@

*@@*@

*@**@

@@@*@

@@**@

0 0

Sample Output

0

1

2

2

题目简介:@表示油井。@只要周围的8个方向上存在@,表示这两个油井是连接的。连接在一起的油井算一个油田。问一共多少个油田。这道题略坑爹的是,输入的两个数字之后有可能存在多个空格,这里需要注意。

方法:深度优先搜索。没有写过深度优先搜索。之前有听过学长讲了一点点栈,结合自己学习的队列的知识,套用在栈上面,就写了一个深搜。当时感觉还不错,后来看了人家用递归写的,发现弱爆了!!!!

#include<iostream>
#include<cstdio>
#include<cstring>
#include<stack>
using namespace std;

struct Stack
{
	int x, y;
	int flag[8];
};

int flag[110][110], m, n;

int main()
{
	int dir[8][2]={{1,0},{-1,0},{0,1},{0,-1},{1,1},{1,-1},{-1,-1},{-1,1}};//表示8个方向
	int i, j, k, sum;
	char map[110];
	
	while(cin>>m>>n)
	{
		if(m + n==0)
			break;
		//输入操作
		int count = 0;
		memset(flag,0,sizeof(flag));
		for(i = 0;i<m;i++)
		{
			scanf("%s",&map);
			for(j = 0;j<n;j++)
			{
				if(map[j]=='@')
				{
					flag[i][j] = 2;
				}
			}
		}
		//‘*’为0,未被处理的‘@’为2,处理了的额为3
		
		stack<struct Stack> S;

		struct Stack tempS, newdS;
		
		for(i = 0;i<m;i++)
		{
			for(j = 0;j<n;j++)
			{
				memset(newdS.flag,0,sizeof(newdS.flag));
		        memset(tempS.flag,0,sizeof(tempS.flag));
				if(flag[i][j]==2)
				{
					tempS.x = i;
					tempS.y = j;
					flag[tempS.x][tempS.y] = 3;
					S.push(tempS);
					while(!S.empty())
					{
						tempS = S.top();
						int flag1 = 0;
						for(k = 0;k<8;k++)
						{
							if(!tempS.flag[k])
							{
								newdS.x = tempS.x + dir[k][0];
								newdS.y = tempS.y + dir[k][1];
								tempS.flag[k] = 1;
								if(newdS.x>=0&&newdS.x<m&&newdS.y>=0&&newdS.y<n)
								{
									if(flag[newdS.x][newdS.y]==2)
									{
										flag[newdS.x][newdS.y] = 3;
										S.push(newdS);
										flag1 = 1;
										break;
									}
								}
							}
						}
						if(flag1==0)
						{
							S.pop();
						}
					}
					count++;
				}
			}
		}
		printf("%d\n",count);
	}
	return 0;
}


 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值