当前位置: 代码迷 >> 综合 >> HDU 1241 Oil Deposits 简单DFS
  详细解决方案

HDU 1241 Oil Deposits 简单DFS

热度:79   发布时间:2024-02-24 00:52:17.0

题目:(来源于hdu oj 官网)
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

ac后的感悟、启发:
这题和上一篇的1010相似,用的DFS思想,不过我觉得比1010简单,没用到depth。
涉及到九网格中要把中心的周围的八个情况都考虑进去。(重要,想到这个就ac了)
这个灵感来源于下图
在这里插入图片描述

简单,很简单的哟!!
不过我的选择又没用到数组,所以篇幅较长,不过还挺容易懂的哦。

#include<iostream>
using namespace std;
int m, n;
char a[101][101];
int visited[101][101];
int result;
void dfs(int x,int y)
{
    int xx, yy;for (int i = 0; i < 8; i++){
    switch (i){
    case 1:xx = x;yy = y + 1;if ( yy > n-1){
    continue;}else{
    if (a[xx][yy] == '@' && visited[xx][yy] == 0){
    visited[xx][yy] = 1;dfs(xx, yy);}}break;case 2:xx = x+1;yy = y - 1;if (xx >m-1 || yy <0){
    continue;}else{
    if (a[xx][yy] == '@' && visited[xx][yy] == 0){
    visited[xx][yy] = 1;dfs(xx, yy);}}break;case 3:xx = x+1;yy = y;if (xx > m - 1){
    continue;}else{
    if (a[xx][yy] == '@' && visited[xx][yy] == 0){
    visited[xx][yy] = 1;dfs(xx, yy);}}break;case 4:xx = x+1;yy = y + 1;if (xx > m - 1 || yy > n - 1){
    continue;}else{
    if (a[xx][yy] == '@' && visited[xx][yy] == 0){
    visited[xx][yy] = 1;dfs(xx, yy);}}break;case 0:xx = x - 1;yy = y + 1;if (xx<0 || yy > n - 1){
    continue;}else{
    if (a[xx][yy] == '@' && visited[xx][yy] == 0){
    visited[xx][yy] = 1;dfs(xx, yy);}}break;case 5:xx = x - 1;yy = y - 1;if (xx<0 || yy <0){
    continue;}else{
    if (a[xx][yy] == '@' && visited[xx][yy] == 0){
    visited[xx][yy] = 1;dfs(xx, yy);}}break;case 6:xx = x - 1;yy = y ;if (xx<0 ){
    continue;}else{
    if (a[xx][yy] == '@' && visited[xx][yy] == 0){
    visited[xx][yy] = 1;dfs(xx, yy);}}break;case 7:xx = x ;yy = y - 1;if (yy<0){
    continue;}else{
    if (a[xx][yy] == '@' && visited[xx][yy] == 0){
    visited[xx][yy] = 1;dfs(xx, yy);}}break;}}
}int main()
{
    while (cin >> m >> n){
    if (!m){
    break;}for (int i = 0; i < m; i++){
    for (int j = 0; j < n; j++){
    cin >> a[i][j];}}memset(visited, 0, sizeof(visited));result = 0;for (int i = 0; i < m; i++){
    for (int j = 0; j < n; j++){
    if (a[i][j] == '@'&& visited[i][j] ==0){
    result++;visited[i][j] = 1;dfs(i, j);}}}cout << result << endl;}return 0;
}