首页 > 代码库 > poj1562深搜

poj1562深搜

#include <cstdio>#include <cstring>#include <algorithm>#include <climits>#include <string>#include <iostream>#include <map>#include <cstdlib>#include <list>#include <set>#include <queue>#include <stack>#define GG getchar();using namespace std;int dx[9]={0,-1,1,0,1,-1,1,-1};int dy[9]={1,0,0,-1,1,-1,-1,1};int n,m;int Map[200][200],vis[200][200];void dfs(int x,int y){  //  if(vis[x][y]) return ;    vis[x][y]=1;    for(int i=0;i<8;i++){        int xx=dx[i]+x;int yy=dy[i]+y;        if(xx>=0&&xx<n&&yy>=0&&yy<m&&!vis[xx][yy]&&Map[xx][yy]){           // cout<<x<<" " <<y<<endl;       // cout<<xx<<" "<<yy<<endl;system("pause");            vis[xx][yy]=1;            dfs(xx,yy);        }    }}int main(){    while(cin>>n>>m,n||m){        GG;        int ans=0;        memset(vis,0,sizeof(vis));        memset(Map,0,sizeof(Map));        for(int i=0;i<n;i++){            for(int j=0;j<m;j++){                char c;                cin>>c;                if(c==‘*‘){                    Map[i][j]=0;                }                else Map[i][j]=1;            }            GG;        }        for(int i=0;i<n;i++){            for(int j=0;j<m;j++){                if(Map[i][j]&&!vis[i][j]){                    ans++;dfs(i,j);                }            }        }        cout<<ans<<endl;    }    return 0;}