标签:
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 13505 | Accepted: 7345 |
Description
Input
Output
Sample Input
1 1 * 3 5 *@*@* **@** *@*@* 1 8 @@****@* 5 5 ****@ *@@*@ *@**@ @@@*@ @@**@ 0 0
Sample Output
0 1 2 2
Source
1 #include<iostream> 2 #include<queue> 3 #include<cstdio> 4 #include<cstring> 5 using namespace std; 6 int m,n; 7 short map[105][105]; 8 short dir[8][2]={{-1,-1},{-1,0},{-1,1},{0,1},{1,1},{1,0},{1,-1},{0,-1}}; 9 bool check(int x,int y){ 10 if(x<0||x>=m||y<0||y>=n||map[x][y]) 11 return false; 12 return true; 13 } 14 int main(){ 15 while(scanf("%d%d",&m,&n)&&m!=0&&n!=0){ 16 memset(map,0,sizeof(map)); 17 int i,j; 18 for(i=0;i<m;i++){ 19 for(j=0;j<n;j++){ 20 char c; 21 cin>>c; 22 if(c==‘*‘){ 23 map[i][j]=-1; 24 } 25 else{ 26 map[i][j]=0; 27 } 28 } 29 } 30 //cout<<1<<endl; 31 /*for(i=0;i<m;i++){ 32 for(j=0;j<n;j++){ 33 cout<<map[i][j]<<endl; 34 } 35 }*/ 36 int count=0; 37 for(i=0;i<m;i++){ 38 for(j=0;j<n;j++){ 39 if(map[i][j]==0){ 40 queue<int> q; 41 q.push(i*100+j); 42 map[i][j]=++count; 43 while(!q.empty()){ 44 short x=q.front()/100; 45 short y=q.front()%100; 46 q.pop(); 47 int k; 48 for(k=0;k<8;k++){ 49 short xx=x+dir[k][0]; 50 short yy=y+dir[k][1]; 51 if(check(xx,yy)){ 52 q.push(xx*100+yy); 53 map[xx][yy]=map[x][y]; 54 } 55 } 56 } 57 } 58 } 59 } 60 /*for(i=0;i<m;i++){ 61 for(j=0;j<n;j++){ 62 cout<<map[i][j]<<endl; 63 } 64 }*/ 65 cout<<count<<endl; 66 } 67 return 0; 68 }
标签:
原文地址:http://www.cnblogs.com/Deribs4/p/4282987.html