SDNU-DFS——B - Oil Deposits

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题

从一个未找过的油田出发往下找,把所有能找的油田全都找了,并且标记已找过

然后从地图上再往下走,知道发现另外一个没有被找过的油田,计数器++

因为每一次查找都有可能往8个方向走,所以可以定义一个步数数组(坐标变化)

判断中除了判断有没有访问过,还要判断有没有越过这个地图范围

注意每一轮的清空,防止对多组数据的影响

#include
#include
#include
#include
using namespace std;

const int maxn = 101;
char oil[maxn][maxn];
int m, n;
int loop[8][2] = {1,0, 1,1, 0,1, -1,0, 0,-1, -1,-1, 1,-1, -1,1};
bool vis[maxn][maxn];

bool check(int x, int y)
{
    if(x>=1 && x<=m && y>=1 && y<=n && oil[x][y] == '@' && !vis[x][y])
        return true;
    return false;
}

void dfs(int x, int y)
{
	int i;
    int temp_x, temp_y;
    for( i = 0; i < 8; i++)
    {
        temp_x = x + loop[i][0];
        temp_y = y + loop[i][1];
        if(check(temp_x, temp_y))
        {
        	vis[temp_x][temp_y] = 1;
            dfs(temp_x, temp_y);
        }
    }
}

int main()
{
	int i,j;
    while(scanf("%d%d", &m, &n), m + n)
    {
        memset(vis, 0, sizeof(vis));
        for(i = 0;i <= m;i++)
        {
        	memset(oil[i],0,sizeof(oil[i]));
		}
        int sign = 0;
        for( i = 1; i <= m; i++)
        {
            for( j = 1; j <= n; j++)
            {
                cin >> oil[i][j];
            }
        }
        for(int i = 1; i <= m; i++)
        {
            for(int j = 1; j <= n; j++)
            {
                if(oil[i][j] == '@' && !vis[i][j])
                {
                    dfs(i, j);
                    sign++;
                }
            }
        }
        cout << sign << endl;
    }

    return 0;
}



你可能感兴趣的:(oj)