标签:des style blog http java color
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5444 Accepted Submission(s): 2341
1 #include <cstdio> 2 #include <cstring> 3 #include <string> 4 #include <algorithm> 5 #include <iostream> 6 using namespace std; 7 8 int father[55*55]; 9 char map[55][55]; 10 11 int findroot(int p){ 12 int r=p; 13 while(r!=father[r]) r=father[r]; 14 int x, j; 15 x=p; 16 while(x!=r){ 17 j=father[x]; 18 father[x]=r; 19 x=j; 20 } 21 return r; 22 } 23 24 25 26 27 main() 28 { 29 int n, m, i, j, k; 30 while(scanf("%d %d",&n,&m)==2&&n!=-1){ 31 for(i=0;i<n;i++) scanf("%s",map[i]); 32 for(i=0;i<=n*m+1;i++) father[i]=i; 33 for(i=0;i<n;i++){ 34 for(j=0;j<m;j++){ 35 if(map[i][j]==‘A‘||map[i][j]==‘B‘||map[i][j]==‘E‘||map[i][j]==‘G‘||map[i][j]==‘H‘||map[i][j]==‘J‘||map[i][j]==‘K‘) 36 { 37 if(i>0&&(map[i-1][j]==‘C‘||map[i-1][j]==‘D‘||map[i-1][j]==‘E‘||map[i-1][j]==‘H‘||map[i-1][j]==‘I‘||map[i-1][j]==‘J‘||map[i-1][j]==‘K‘)) 38 { //向上 39 int fx=findroot(i*m+j); 40 int fy=findroot((i-1)*m+j); 41 if(fx!=fy) father[fy]=fx; 42 } 43 } 44 if(map[i][j]==‘B‘||map[i][j]==‘D‘||map[i][j]==‘F‘||map[i][j]==‘G‘||map[i][j]==‘I‘||map[i][j]==‘J‘||map[i][j]==‘K‘) 45 { 46 if(j<m-1&&(map[i][j+1]==‘A‘||map[i][j+1]==‘C‘||map[i][j+1]==‘F‘||map[i][j+1]==‘G‘||map[i][j+1]==‘H‘||map[i][j+1]==‘I‘||map[i][j+1]==‘K‘)) 47 { //向右 48 int fx=findroot(i*m+j); 49 int fy=findroot(i*m+j+1); 50 if(fx!=fy) father[fy]=fx; 51 } 52 } 53 54 55 } 56 } 57 int num=0; 58 for(i=0;i<n*m;i++) 59 if(father[i]==i) num++; 60 // printf("%d ",father[i]); 61 printf("%d",num); 62 printf("\n"); 63 64 } 65 }
标签:des style blog http java color
原文地址:http://www.cnblogs.com/qq1012662902/p/3852411.html