hdu 1241dfs Oil Deposits

hdu 1241dfs Oil Deposits

 

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 10610    Accepted Submission(s): 6151

 

 

Problem Description

The GeoSurvComp geologic survey company isresponsible for detecting underground oil deposits. GeoSurvComp works with onelarge rectangular region of land at a time, and creates a grid that divides theland into numerous square plots. It then analyzes each plot separately, usingsensing equipment to determine whether or not the plot contains oil. A plotcontaining oil is called a pocket. If two pockets are adjacent, then they arepart of the same oil deposit. Oil deposits can be quite large and may containnumerous pockets. Your job is to determine how many different oil deposits arecontained 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 columnsin the grid, separated by a single space. If m = 0 it signals the end of theinput; otherwise 1 <= m <= 100 and 1 <= n <= 100. Following thisare m lines of n characters each (not counting the end-of-line characters).Each character corresponds to one plot, and is either `*', representing the absenceof oil, or `@', representing an oil pocket.

 

 

Output

For each grid, output the number ofdistinct oil deposits. Two different pockets are part of the same oil depositif they are adjacent horizontally, vertically, or diagonally. An oil depositwill 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來解決的,很簡單每個點都搜它的八個方位,如果存在解決@則深搜下去。用visit記錄。

其實時間複雜度很小,因爲n,m<100,計算量最大也就是100*100*8。不存在超時的情況。

源代碼:

#include <iostream>

#include <stdio.h>

#include <string.h>

#include <string>

using namespacestd;

int n,m;

char map[105][105];

bool visit[105][105];

int dir[8][2] ={{1,0},{-1,0},{0,-1},{0,1},{1,1},{1,-1},{-1,1},{-1,-1}};

 

bool judge(intx,int y)

{

      if(x>= n ||x < 0||y >= m||y < 0)

           returnfalse;

      if(map[x][y]== '*')

           returnfalse;

      if(visit[x][y])

           returnfalse;

 

      returntrue;

}

void dfs(intx,int y)

{

      visit[x][y]= true;

      intxx,yy;

      for(inti = 0;i < 8;i++)

      {

           xx= x + dir[i][0];

           yy= y + dir[i][1];

           if(judge(xx,yy))

                 dfs(xx,yy);

      }

}

int main()

{

      while(scanf("%d%d",&n,&m)!= EOF&&m)

      {

           getchar();

           memset(visit,false,sizeof(visit));

           for(inti = 0;i < n;i++)

           {

                 scanf("%s",map[i]);

           }

 

           intnum = 0;

           for(inti = 0;i < n;i++)

                 for(intk = 0;k < m;k++)

                      if(!visit[i][k]&& map[i][k] == '@')

                      {

                            num++;

                            dfs(i,k);

                      }

           printf("%d\n",num);

      }

      return0;

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章