hdu 1241 oil deposits

搜索一直学的很水= =

有时灵光有时不灵光= =

决定花两天时间复习一下,把以前水过的题目再整理整理,别贱笑了...

题目意思很简单,问有几片油田(只要某点的八个临位上也有‘@’就表示两者是连在一起的,算一块)

很简单的DFS,入门级别,只要先找到一个‘@’然后深搜下去,就行,直接代码

(这题有个问题,输入的时候要读掉多余的空行= =麻烦,一开始还没发现)

#include <iostream>
#include <cstring>
#include <cstdio>
#include <string>
#include <algorithm>
using namespace std;
int m,n;
char map[101][101];
int dir[8][2]={{1,0},{1,1},{1,-1},{0,1},{0,-1},{-1,1},{-1,-1},{-1,0}};
void dfs(int x,int y)
{
    int fx,fy;
    for(int i=0;i<8;i++)
    {
        fx=x+dir[i][0];
        fy=y+dir[i][1];
        if(fx>=0&&fx<n&&fy>=0&&fy<m&&map[fx][fy]=='@')
        {
            map[fx][fy]='*';
            dfs(fx,fy);
        }
    }
}
int main()
{
    int cnt=0;
    //freopen("in.txt","r",stdin);
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        getchar();
        if(n==0&&m==0)
            break;
        cnt=0;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                scanf("%c",&map[i][j]);
            }
            getchar();
        }
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++)
        {
            if(map[i][j]=='@')
            {
                map[i][j]='*';
                cnt++;
                dfs(i,j);
            }
        }
        printf("%d\n",cnt);
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值