Oil Deposits(DFS,基礎題)

御史大夫發表於2012-09-02

Oil Deposits

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

原題連結:點選開啟連結


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
 
開始時想用並查集。。。後來還是深搜算了,程式很快寫出來,可是測試例子的時候卻發現有誤,debug半天才發現原來是複製網站上的例子的時候換行符不知道為何沒複製過去。。。
AC CODE
//Memory: 344 KB 		Time: 0 MS
//Language: C++ 		Result: Accepted

#include <iostream>
#include <string>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <queue>
#define LL long long
#define MAXI 2147483647
#define MAXL 9223372036854775807
#define dg(i) cout << "*" << i << endl;
using namespace std;

typedef struct Node
{
    int x, y;
    Node(int i, int j)
    {
        x = j; y = i;
    }
}P;
queue<Node> Q;
int n, m;
char grid[101][101];
int move[8][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}, {1, 1}, {-1, 1}, {1, -1}, {-1, -1}};

void DFS(int i, int j)
{
    int ii, jj, k;
    for(k = 0; k < 8; k++)
    {
        ii = i + move[k][0];
        jj = j + move[k][1];
        if(ii > -1 && jj > -1 && ii < m && jj < n && grid[ii][jj] == '@')
        {
            grid[ii][jj] = '*';
            DFS(ii, jj);
        }
    }
}

int main()
{
    int i, j, cnt;
    char c;
    while(scanf("%d %d", &m, &n) && m)
    {
        cnt = 0;
        for(i = 0; i < m; i++)
        {
            scanf("%c", &c); //吃掉換行符
            for(j = 0; j < n; j++)
            {
                scanf("%c", &grid[i][j]);
                if(grid[i][j] == '@')
                {
                    P t(i, j);
                    Q.push(t);
                }
            }
        }
        while(!Q.empty())
        {
            P t = Q.front();
            Q.pop();
            if(grid[t.y][t.x] == '@')
            {
                cnt++;
                DFS(t.y, t.x);
            }
        }
        printf("%d\n", cnt);
    }
    return 0;
}



相關文章