hdu 1241 Oil Deposits (一次dfs搞定有某有)

简介:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
char map[105][105];

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

bool judge(int x, int y){
      if(x<1 || x>n || y<1 || y>m) return false;
      return true;
}

void dfs(int x, int y, bool flag){

   if(map[x][y]=='@'){
          map[x][y]='*';
          ++ans;
       for(int i=0; i<8; ++i){
             int xx=x+dir[i][0], yy=y+dir[i][1];
             if(judge(xx, yy) && map[xx][yy]=='@'){
                  --ans;
             dfs(xx, yy, 1);
          }
       }
       if(flag)  return;//flag==1表明没有将油田搜索完毕
   }
   
   for(int i=0; i<8; ++i){
          int xx=x+dir[i][0], yy=y+dir[i][1];  
            if(judge(xx, yy) && map[xx][yy]!='#'){
              if(map[xx][yy]=='*')
                 map[xx][yy]='#';
              dfs(xx, yy, 0);
          }
   }
}

int main(){
    while(cin>>n>>m && (n||m)){
        for(int i=1; i<=n; ++i)
           cin>>(map[i]+1);
        ans=0;
        dfs(1, 1, 0);
        cout<<ans<<endl;
    }
    return 0;
}

目录
相关文章
|
算法
1091 zoj Knight Moves的BFS算法和DFS
1091 zoj Knight Moves的BFS算法和DFS
56 0
AtCoder Beginner Contest 226 E - Just one(dfs求连通块 组合数学)
AtCoder Beginner Contest 226 E - Just one(dfs求连通块 组合数学)
104 0
POJ-2488,A Knight's Journey(DFS)
POJ-2488,A Knight's Journey(DFS)
HDOJ/HDU 1241 Oil Deposits(经典DFS)
HDOJ/HDU 1241 Oil Deposits(经典DFS)
86 0