杭电1241

来源:互联网 发布:最新网络流行词汇 编辑:程序博客网 时间:2024/06/14 06:14

Oil Deposits
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 32019 Accepted Submission(s): 18580

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.
题目的意思,相邻的油块算是同一块油田(上下左右,左上左下,右上右下一共八个方向),问一共有多少油田,本题就是常规的深搜。没什么要讲解的,读懂题意即可;由原来的四个方向搜索,改为八个方向而已,不多说,直接上代码吧。


#include"iostream"#include "vector"#include"set"using std::cin;using std::cout;using std::endl;int m, n;char map[105][105];void dfs(int x, int y){    int next[8][2] = { { -1,-1 },{ 0,-1 },{ 1,-1 },{ 1,0 },{ 1,1 },{ 0,1 },{ -1,1 },{ -1,0 } };    if (x < 0 || x>m - 1 || y < 0 || y>n - 1)        return;    map[x][y] = '*';    for (int k = 0; k < 8; k++)    {        int nx = x + next[k][0];        int ny = y + next[k][1];        if(map[nx][ny]=='@')            dfs(nx, ny);    }}int main(){    while (cin >> m >> n)    {        int s=0;        if (m == 0)            break;        for (int i = 0; i < m; i++)        {            for (int j = 0; j < n; j++)                cin >> map[i][j];        }        for (int i = 0; i < m; i++)        {            for (int j = 0; j < n; j++)                if (map[i][j] == '@')                {                    int flag=1;                    dfs(i, j);                    s++;                }        }        cout << s<< endl;    }    return 0;}

这就是AC代码了,常规的深搜,没什么好说的!!!
希望小太阳能给你们一些帮助。。。

原创粉丝点击