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;
}


峰注:不明白请留言

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值