航院1241 Oil-Deposit

Problem C - Oil Deposits

题目来源:https://vjudge.net/contest/207868#problem/C

Problem description:

TheGeoSurvComp geologic survey company is responsible for detecting undergroundoil deposits. GeoSurvComp works with one large rectangular region of land at atime, and creates a grid that divides the land into numerous square plots. Itthen analyzes each plot separately, using sensing equipment to determinewhether 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. Oildeposits can be quite large and may contain numerous pockets. Your job is todetermine how many different oil deposits are contained in a grid.

 

Input

The input contains one or moregrids. Each grid begins with a line containing m and n, the number of rows andcolumns in the grid, separated by a single space. If m = 0 it signals the endof the input; otherwise 1 <= m <= 100 and 1 <= n <= 100. Followingthis are m lines of n characters each (not counting the end-of-linecharacters). Each character corresponds to one plot, and is either `*',representing the absence of oil, or `@', representing an oil pocket.

Output

are adjacent horizontally, vertically, or diagonally. An oil deposit willnot contain more than 100 pockets.

Sample Input

1 1

*

3 5

*@*@*

**@**

*@*@*

1 8

@@****@*

5 5

****@

*@@*@

*@**@

@@@*@

@@**@

0 0

Sample Output

0

1

2

2

 

题意描述:将一个矩阵中任何横,竖或斜着相连的‘@’字符当成一个量,计算一共有几个量。

 

解题思路:这是一个典型的深度优先搜索的题,对每一个含有‘@’的字符进行深度优先搜索即可。

 

AC代码:

#include<stdio.h>
#include<string.h>
char a[105][105];
int book[105][105];
int m, n;
 
void dfs(int x, int y)
{
      intnext[8][2] = {{0, 1},
                                {1, 0},
                                {0,-1},
                                {-1,0},
                                {-1,-1},
                                {-1, 1},
                                {1, -1},
                                {1, 1}};
      int k, tx,ty;
      a[x][y] ='*';
      for(k  = 0; k <= 7; k ++)
      {
            tx =x + next[k][0];
            ty =y + next[k][1];
            if(tx< 0 || tx > n - 1 || ty < 0 || ty > m - 1)
            {
                  continue;
            }
            if(a[tx][ty]== '@' && book[tx][ty] == 0)
            {
                  book[tx][ty]= 1;
                  dfs(tx,ty);
            }
      }
      return; 
} 
 
int main(void)
{
      int i, j,num;
      while(scanf("%d%d",&n, &m) != EOF)
      {
            memset(book,0, sizeof(book));
            getchar();
            if(n== 0)
            {
                  break;
            }
            num =0;
            for(i= 0; i <= n - 1; i ++)
            {
                  scanf("%s",a[i]);
            }
            for(i= 0; i <= n - 1; i ++)
            {
                  for(j= 0; j <= m - 1; j ++)
                  {
                        if(a[i][j]== '@')
                        {
                              num++;
                              book[i][j]= 1;
                              dfs(i,j);
                        }
                  }
            }
            printf("%d\n",num);
      }
      return 0;
}

错误分析:

1:因为是多实例,忘记了对标记变量进行初始化。

2:在输入for循环时使用了%s,但是我写的for循环从1开始,忘记了%s默认从0开始。

posted @ 2018-01-23 17:19  moonlight987  阅读(168)  评论(0编辑  收藏  举报