zoukankan      html  css  js  c++  java
  • DFS

    http://poj.org/problem?id=2386
    
    
    #include <iostream>
    #include <cstdio>
    using namespace std;
    const int MAX = 10000;
    char Map[MAX][MAX];
    int N,M;
    int d[8][2]={{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}};
    bool overwall(int nx,int ny)
    {
        if(nx>=0&&nx<N&&ny>=0&&ny<M)
        return true;
        else
        return false;
    }
    void dfs(int x,int y)
    {
        Map[x][y]='.';
        for(int k=0;k<8;k++)
        {
            int tx=x+d[k][0];
            int ty=y+d[k][1];
            if(overwall&&Map[tx][ty]=='W')
            dfs(tx,ty);
        }
        return ;
    }
    void solve()
    {
        int ans=0;
        for(int i=0;i<N;i++)
        for(int j=0;j<M;j++)
        {
            if(Map[i][j]=='W')
            {
                dfs(i,j);
                ans++;
            }
        }
        cout<<ans<<endl;
    }
    int main()
    {
        while(cin>>N>>M)
        {
            for(int i=0;i<N;i++)
            {
                for(int j=0;j<M;j++)
                {
                    cin>>Map[i][j];
                }
            }
            solve();
        }
        return 0;
    }
  • 相关阅读:
    web--ajax--json
    4.26
    4.25
    4.23
    4.22
    4.20
    4.19
    4.18
    4月问题总结章
    4.17
  • 原文地址:https://www.cnblogs.com/xiao-xue-di/p/9441653.html
Copyright © 2011-2022 走看看