1 1
*
3 5
*@*@*
**@**
*@*@*
1 8
@@****@*
5 5
****@
*@@*@
*@**@
@@@*@
@@**@
0 0
//#include<bits/stdc++.h> #include<iostream> #include<cstdio> #include<vector> #include<stack> #include<cstring> #include<queue> #include<algorithm> using namespace std; const int maxn=105; const int inf=200000; #define lson rt<<1,l,m #define rson rt<<1|1,m+1,r #define For(i,n) for(int i=0;i<(n);i++) template<class T>inline T read(T&x) { char c; while((c=getchar())<=32); bool ok=false; if(c=='-')ok=true,c=getchar(); for(x=0; c>32; c=getchar()) x=x*10+c-'0'; if(ok)x=-x; return x; } template<class T> inline void read_(T&x,T&y) { read(x); read(y); } template<class T> inline void write(T x) { if(x<0)putchar('-'),x=-x; if(x<10)putchar(x+'0'); else write(x/10),putchar(x%10+'0'); } template<class T>inline void writeln(T x) { write(x); putchar('\n'); } // -------IO template------ struct node { int x,y; node(int a,int b){x=a;y=b;} }; int dx[]={0,0,1,-1,1,-1,1,-1}; int dy[]={-1,1,0,0,-1,1,1,-1}; bool vis[maxn][maxn]; char a[maxn][maxn]; int n,m; void bfs(int x,int y) { queue<node> q; vis[x][y]=true; q.push(node(x,y)); while(!q.empty()) { node s=q.front();q.pop(); For(i,8) { int xx=dx[i]+s.x; int yy=dy[i]+s.y; if(xx>=n||xx<0||yy>=m||yy<0||a[xx][yy]!='@'||vis[xx][yy])continue; q.push(node(xx,yy)); vis[xx][yy]=true; } } } int main() { #ifndef ONLINE_JUDGE freopen("in.txt","r",stdin); #endif // ONLINE_JUDGE int i,j,k,t; while(~scanf("%d%d",&n,&m)&&(n||m)) { For(i,n)scanf("%s",a[i]); int ans=0; memset(vis,0,sizeof(vis)); For(i,n)For(j,m) { if(!vis[i][j]&&a[i][j]=='@') { bfs(i,j); ans++; } } writeln(ans); } return 0; }
L - Oil Deposits HDU 1241 基础BFS
原文:http://blog.csdn.net/u013167299/article/details/44652477