hdu 1241 Oil Deposits(经典dfs)

题目:http://acm.hdu.edu.cn/showproblem.php?pid=1241

大意:寻找一块区域中有多少的‘@’域,左右上下还有四个斜45度的相邻点都算作连起来的。

Sample Input
  
  
1 1 * 3 5 *@*@* **@** *@*@* 1 8 @@****@* 5 5 ****@ *@@*@ *@**@ @@@*@ @@**@ 0 0
 

Sample Output
  
  
0 1 2 2
代码:
#include <iostream>
#include<cstdio>
using namespace std;
char s[105][105];
int n,m;
int dir[8][2]={{0,1},{0,-1},{-1,0},{1,0},{1,1},{-1,1},{1,-1},{-1,-1}};
void show(){
    cout<<"show:"<<endl;
    for(int i=0;i<n;i++){
       for(int j=0;j<m;j++){
           cout<<s[i][j];
       }
       cout<<endl;
    }
}
void dfs(int x,int y){
    s[x][y]='*';
    for(int i=0;i<8;i++){
        int xx=x+dir[i][0],yy=y+dir[i][1];
        if(xx<n&&xx>=0&&yy<m&&yy>=0&&s[xx][yy]=='@'){
             dfs(xx,yy);
             //show();
        }
    }
}
int main()
{
    //freopen("cin.txt","r",stdin);
    while(cin>>n>>m){
        if(n==0&&m==0)break;
         for(int i=0;i<n;i++){
             scanf("%s",s[i]);
         }
         int ans=0;
         for(int i=0;i<n;i++){
             for(int j=0;j<m;j++){
                 if(s[i][j]=='@'){
                     dfs(i,j);
                     ans++;
                 }
             }
         }
         printf("%d\n",ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值