油田问题
#include <iostream> using namespace std; const int N=50; const int M=50; char a[N][M]; bool pathed[N][M];//留下足迹数组 short int p[4][2]={-1,0,0,1,1,0,0,-1};//行走方向数组 int n,m;//行列变量 int dfs(int,int); int main() { int i,j,sum; while (scanf("%d%d",&n,&m)!=EOF) { sum=0; memset(pathed,0,sizeof(pathed)); for (i=0;i<n;i++) scanf("%s",a[i]); for (i=0;i<n;i++) for (j=0;j<m;j++) if (pathed[i][j]==0&&a[i][j]=='@')//搜索初始条件 { sum+=dfs(i,j); } printf("%d\n",sum); } } int dfs(int i,int j) { int row,col,k; pathed[i][j]=1; //留足迹 for (k=0;k<4;k++) { row=i+p[k][0], col=j+p[k][1]; if (row>=0 && row<n && col>=0 && col<m && a[row][col]=='@' && pathed[row][col]==0) //搜索条件,非常重要 dfs(row,col); } return 1; //可以不要返回值,主程序怎么改? }