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

时间:2023-03-08 18:12:16
hdu 1241 Oil Deposits (一次dfs搞定有某有)
 #include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
char map[][]; int dir[][]={, , , , -, , , -, , , , -, -, , -, -};
int n, m;
int ans; bool judge(int x, int y){
if(x< || x>n || y< || 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=; i<; ++i){
int xx=x+dir[i][], yy=y+dir[i][];
if(judge(xx, yy) && map[xx][yy]=='@'){
--ans;
dfs(xx, yy, );
}
}
if(flag) return;//flag==1表明没有将油田搜索完毕
} for(int i=; i<; ++i){
int xx=x+dir[i][], yy=y+dir[i][];
if(judge(xx, yy) && map[xx][yy]!='#'){
if(map[xx][yy]=='*')
map[xx][yy]='#';
dfs(xx, yy, );
}
}
} int main(){
while(cin>>n>>m && (n||m)){
for(int i=; i<=n; ++i)
cin>>(map[i]+);
ans=;
dfs(, , );
cout<<ans<<endl;
}
return ;
}