zoj 1709 Oil Deposits

题目 点击打开链接
#include <iostream>
#include <cstring>

using namespace std;
const int MAXN = 105;

char map1[MAXN][MAXN];
int dir[8][2] = { {-1, 0}, {-1, -1}, {0, -1}, {1, -1}, {1, 0}, {1, 1}, {0, 1}, {-1, 1} };
int m, n;
void DFS( int x, int y )
{
     map1[x][y] = '*';
     for(int i = 0; i < 8; ++i)
     {
          int x1 = x + dir[i][0];
          int y1 = y + dir[i][1];
          if( x1 < 0 || y1 < 0 || x1 >= m || y1 >= n )
               continue;
          if(map1[x1][y1] == '@')
               DFS(x1, y1);
     }
}

int main()
{
     int cnt;
     int i, j;
     while(1)
     {
          cin>>m>>n;
          if(m == 0)
               break;
          memset(map1, 0, sizeof(map1));
          for(i = 0; i < m; ++i)
               cin>>map1[i];
          cnt = 0;
          for(i = 0; i < m; ++i)
          {
               for(j = 0; j < n; ++j)
               {
                    if(map1[i][j] == '@')
                         DFS(i, j), cnt++;
               }
          }
          cout<<cnt<<endl;
     }
    return 0;
}

你可能感兴趣的:(DFS)