做的第一道广搜题,纪念一下!!!
1 #include<iostream> 2 #include<cstdio> 3 #include<queue> 4 using namespace std; 5 6 const int N=1005; 7 int r,c,ans,a[N][N],t[]={-1,1,0,0,0,0,-1,1}; 8 queue<int> q; 9 void bfs(){ 10 if(q.empty()){ 11 ans++; 12 return; 13 } 14 int ni=q.front(); 15 q.pop(); 16 int nj=q.front(); 17 q.pop(); 18 a[ni][nj]=0; 19 for(int i=0;i<4;i++){ 20 ni+=t[i],nj+=t[i+4]; 21 if(ni>0&&ni<=r&&nj>0&&nj<=c&&a[ni][nj]) 22 q.push(ni),q.push(nj); 23 ni-=t[i],nj-=t[i+4]; 24 } 25 bfs(); 26 } 27 int main(){ 28 cin>>r>>c; 29 char cc; 30 for(int i=1;i<=r;i++) 31 for(int j=1;j<=c;j++){ 32 cin>>cc; 33 a[i][j]=cc-'0'; 34 } 35 for(int i=1;i<=r;i++) 36 for(int j=1;j<=c;j++) 37 if(a[i][j]){ 38 q.push(i),q.push(j); 39 bfs(); 40 } 41 cout<<ans; 42 return 0; 43 }