经过一个晚上和am我终于把这鬼东东搞出来了,yep

#include<bits/stdc++.h>
using namespace std;
int n,m;
char s;
int a[101][101],x[4]={0,0,1,-1},y[4]={1,-1,0,0},b[101][3],sum=0;
void bfs(int i,int j){
    int head=0,foot=1,xx,yy;
    a[i][j]=0;
    b[1][1]=i;
    b[1][2]=j;
    sum++;
    do{
        head++;
        for(int q=0;q<=3;q++){//哇,要注意有等号啊,坑死我啦 
            xx=b[head][1]+x[q];
            yy=b[head][2]+y[q];
            if((xx>=0)&&(xx<n)&&(yy>=0)&&(yy<m)&&(a[xx][yy]==1)){
                foot++;
                b[foot][1]=xx;
                b[foot][2]=yy;
                a[xx][yy]=0;
            }
        }
    }
    while(head<foot);
}
int main(){
    cin>>n>>m;
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            cin>>s;
            if(s-'0'>=1&&s-'0'<=9){ //注意这里是&&不是|| 
                a[i][j]=1;
            }
        }
    }
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            if(a[i][j]==1) 
                bfs(i,j);
        
}
}cout<<sum;
}

 

posted on 2018-08-02 10:53  cjoier__zjing  阅读(240)  评论(0编辑  收藏  举报