zoukankan      html  css  js  c++  java
  • hdu 杭电 1241 Oil Deposits 果枫

    题意:找图中没有连在一起的'@'的个数。

    解法:广搜

    ac代码:

    View Code
    #include<iostream>
    #include<queue>
    using namespace std;
    
    const int M=100+10;
    char map[M][M];//地图
    bool use[M][M];//用作标记
    int vec[8][2]={-1, 0,
    -1,-1,
    0,-1,
    1,-1,
    1, 0,
    1, 1,
    0, 1,
    -1,1};//方向向量:左,左上,上,右上,右,右下,下,左下
    
    struct que
    {
        int i,j;
    };
    
    int main()
    {
        int m,n;
        int i,j,k;
        int count;
        int d,c;
        queue<que>q;
        que temp;
        while(scanf("%d%d",&m,&n)!=EOF&&m)
        {
            getchar();
            count=0;
            memset(use,0,sizeof(use));
            for(i=1;i<=m;i++)
            {
                for(j=1;j<=n;j++)
                {
                    scanf("%c",&map[i][j]);
                    if(map[i][j]=='@')
                        use[i][j]=1;
                }
                getchar();
            }
    
            for(i=1;i<=m;i++)
            {
                for(j=1;j<=n;j++)
                {
                    if(use[i][j])
                    {
                        use[i][j]=0;
                        temp.i=i;temp.j=j;
                        q.push(temp);
                        while(!q.empty())
                        {
                            for(k=0;k<8;k++)
                            {
                                d=q.front().i+vec[k][0];
                                c=q.front().j+vec[k][1];
                                if((d>=1&&d<=m)&&(c>=1&&c<=n)&&use[d][c]!=0)
                                {
                                    temp.i=d;temp.j=c;
                                    q.push(temp);
                                    use[d][c]=0;
                                }
                            }
                            q.pop();
                        }
                        count++;
                    }
                }
            }
            printf("%d\n",count);
        }
        return 0;
    }
    
    
    峰注:不明白请留言
  • 相关阅读:
    Spring事务隔离级别、传播机制、实现方式
    包装类缓存
    Object类详解
    String类详解
    自己实现一个Map
    锁机制
    各容器区别比较
    Map-CurrentHashMap
    Javascript中bind()方法的使用与实现
    this、new、call和apply的相关问题
  • 原文地址:https://www.cnblogs.com/zgfailmr/p/2665887.html
Copyright © 2011-2022 走看看