这道题还是写了比较久的,最近广搜的题目是真的有点复杂了
#include<bits/stdc++.h> using namespace std; int b[1010][1010],l[1010],n,m,xx,yy,head,tail, dx[5]={0,-1,1,0,0},dy[5]={0,0,0,-1,1},num=0,h=1,v=1,s[1010][1010]; struct JF{ int x,y,step; }YY[10000001]; int main() { cin>>n>>m; char ch; for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) { cin>>ch; if(ch=='0') b[i][j]=0; else b[i][j]=ch-'0'; } for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) { if(b[i][j]==0)continue; num++; head=0,tail=1; YY[1].x=i; YY[1].y=j; b[i][j]=0; while(head<tail) { head++; for(int k=1;k<=4;k++) { int xx=YY[head].x+dx[k]; int yy=YY[head].y+dy[k]; // cout<<" "; <<" "<<num<<" "<<head<<" "<<tail // // cout<<xx<<" "<<dx[i]<<" "<<YY[head].x<<" "<<yy<<" "<<dy[i]<<" "; if((xx>0)&&(xx<=n)&&(yy>0)&&(yy<=m)&&(b[xx][yy]!=0)) { tail++; b[xx][yy]=0; YY[tail].x=xx; YY[tail].y=yy; } } } } cout<<num; return 0; }