现在的位置: 首页 > 综合 > 正文

13.04.07 Oil Deposits (DFS)

2018年01月12日 ⁄ 综合 ⁄ 共 2494字 ⁄ 字号 评论关闭

Oil Deposits

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 46   Accepted Submission(s) : 20

Font: Times New Roman | Verdana | Georgia

Font Size:  

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

算法
	深度优先搜索(DFS)

思路:
	从a[0][0]位置开始搜索,一直到a[m-1][n-1]。
	找到有oil pocket 并标记为已搜索过。然后向其上下左右以及斜对角八个方向前进,如果有oil pocket,并且没有标记过,则跳到该位置,并继续	向下搜索。当该位置无法前进时,则回到上一个节点,然后换一个方向继续。
	当搜索完一块区域后,跳到下一个区域。

代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
using namespace std;
int x,y,a[105][105],map[105][105],t;
int go(int i, int j)
{
    int k;
    if (map[i][j]==1)
	{
		map[i][j]=0;
		if (a[i+1][j]==1) go(i+1,j);
		if (a[i-1][j]==1) go(i-1,j);
		if (a[i][j+1]==1) go(i,j+1);
		if (a[i][j-1]==1) go(i,j-1);
		if (a[i+1][j+1]==1) go(i+1,j+1);
		if (a[i+1][j-1]==1) go(i+1,j-1);
		if (a[i-1][j+1]==1) go(i-1,j+1);
		if (a[i-1][j-1]==1) go(i-1,j-1);


	}
	return t;
}
int main ()
{
    int i,j,w,h;
    char ch;
    while (cin>>x)
    {
        cin>>y;
        if (x==0) break;
        t=0;
        memset(a,0,sizeof(a));
        memset(map,1,sizeof(map));
        for (i=0; i<x; i++)
        {
            for (j=0; j<y; j++)
            {
                cin>>ch;
                if (ch=='@')
                    a[i][j]=1;
                else if (ch=='*')
                    a[i][j]=0;
                map[i][j]=1;
            }
        }
        for (i=0; i<x; i++)
        {
            for (j=0; j<y; j++)
            {
                if (map[i][j]==1 && a[i][j]==1)
                {
                    go(i,j);
                    t++;
                }
            }
        }
        cout<<t<<endl;
    }
    return 0;
}
	
	


抱歉!评论已关闭.