Skip to content

Latest commit

 

History

History
125 lines (118 loc) · 3.69 KB

hdu1241.MD

File metadata and controls

125 lines (118 loc) · 3.69 KB

日期 2021 / 10 / 28

题目

[题目链接]https://acm.dingbacode.com/showproblem.php?pid=1241 截屏2021-10-28 20 03 46

Oil Deposits

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

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,我们可以对每一个点进行遍历,从某个点遍历时当他的点是@时,我们把他设为#理解为这个点走过了 然后利用一个计数器计数,得到答案,我刚开始不理解void dfs这个函数怎么结束,因为我没有加return,后面才知道在一个for循环内如果没有一个满足的条件,就不会进入下一个dfs 此时函数就回到上一个,一直到最开始的函数结束.

代码演示

#include <bits/stdc++.h>

#define INF 0x7fffffff
#define rep(x, y, z) for (int x = y; x <= z; x++)
#define dec(x, y, z) for (int x = y; x >= z; x--)
#define format(a) memset (a, 0, sizeof(a))
#define swap(a, b) (a ^= b ^= a ^= b)
#define ll long long int
#define ull unsigned long long int 
#define uint unsigned int
const int maxn = 1e6 + 10;
using namespace std;

int dir[][2] = { {-1, -1}, {-1, 0}, {-1, 1}, {0, 1}, {0, -1}, {1, -1}, {1, 0}, {1, 1}};
char map1[1100][1100];

void dfs(int x, int y) {
  for (int i = 0; i < 8; i++) {
    int dx = x + dir[i][0];
    int dy = y + dir[i][1];
    if (map1[dx][dy] == '@') {
      map1[dx][dy] = '#';
      dfs(dx, dy);
    }
  }
}

int main(int argc, char *argv[]) {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, m, cnt;
  while (cin >> n >> m && n) {
    cnt = 0;
    for (int i = 0; i < n; i++) {
      cin >> map1[i];
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (map1[i][j] == '@') {
	  map1[i][j] = '#';
	  dfs(i, j);
	  cnt++;
	}
      }
    }
    cout << cnt << endl;
  }
  return 0;
}