//直接打表了,connect[x][y][z]表示x的z方向上的y,他们之间是否连通,x,y的取值为0-10, //分别代表那11中类型,z表示方向0表示右方,1表示上方,2表示左方,3表示下方 //7236312 2012-11-23 22:37:23 Accepted 1198 15MS 1552K 2455 B G++ chen #include<stdio.h> #include<stdlib.h> #include<string.h> int N,M; int connect[11][11][4]={ {{0,0,0,0},{0,0,1,0},{0,1,0,0},{0,1,1,0},{0,1,0,0},{0,0,1,0},{0,0,1,0},{0,1,0,0},{0,1,1,0},{0,1,1,0},{0,1,1,0}}, {{1,0,0,0},{0,0,0,0},{1,1,0,0},{0,1,0,0},{0,1,0,0},{1,0,0,0},{1,0,0,0},{1,1,0,0},{1,1,0,0},{0,1,0,0},{1,1,0,0}}, {{0,0,0,1},{0,0,1,1},{0,0,0,0},{0,0,1,0},{0,0,0,1},{0,0,1,0},{0,0,1,1},{0,0,0,1},{0,0,1,0},{0,0,1,1},{0,0,1,1}}, {{1,0,0,1},{0,0,0,1},{1,0,0,0},{0,0,0,0},{0,0,0,1},{1,0,0,0},{1,0,0,1},{1,0,0,1},{1,0,0,0},{0,0,0,1},{1,0,0,1}}, {{0,0,0,1},{0,0,0,1},{0,1,0,0},{0,1,0,0},{0,1,0,1},{0,0,0,0},{0,0,0,1},{0,1,0,1},{0,1,0,0},{0,1,0,1},{0,1,0,1}}, {{1,0,0,0},{0,0,1,0},{1,0,0,0},{0,0,1,0},{0,0,0,0},{1,0,1,0},{1,0,1,0},{1,0,0,0},{1,0,1,0},{0,0,1,0},{1,0,1,0}}, {{1,0,0,0},{0,0,1,0},{1,1,0,0},{0,1,1,0},{0,1,0,0},{1,0,1,0},{1,0,1,0},{1,1,0,0},{1,1,1,0},{0,1,1,0},{1,1,1,0}}, {{0,0,0,1},{0,0,1,1},{0,1,0,0},{0,1,1,0},{0,1,0,1},{0,0,1,0},{0,0,1,1},{0,1,0,1},{0,1,1,0},{0,1,1,1},{0,1,1,1}}, {{1,0,0,1},{0,0,1,1},{1,0,0,0},{0,0,1,0},{0,0,0,1},{1,0,1,0},{1,0,1,1},{1,0,0,1},{1,0,1,0},{0,0,1,1},{1,0,1,1}}, {{1,0,0,1},{0,0,0,1},{1,1,0,0},{0,1,0,0},{0,1,0,1},{1,0,0,0},{1,0,0,1},{1,1,0,1},{1,1,0,0},{0,1,0,1},{1,1,0,1}}, {{1,0,0,1},{0,0,1,1},{1,1,0,0},{0,1,1,0},{0,1,0,1},{1,0,1,0},{1,0,1,1},{1,1,0,1},{1,1,1,0},{0,1,1,1},{1,1,1,1}} }; int dir[4][2]={{0,1},{-1,0},{0,-1},{1,0}}; int mat[550][550]; int visited[550][550]; void dfs(int a,int b){ visited[a][b]=1; for(int i=0;i<4;i++){ int m=a+dir[i][0]; int n=b+dir[i][1]; if(m<M&&m>=0&&n<N&&n>=0&&visited[m][n]==0){ if(connect[mat[a][b]][mat[m][n]][i]==1) dfs(m,n); } } } int main(){ while(scanf("%d%d",&M,&N)!=EOF){ if(M<0||N<0) break; memset(visited,0,sizeof(visited)); getchar(); char a; for(int i=0;i<M;i++){ for(int j=0;j<N;j++){ scanf("%c",&a); mat[i][j]=a-'A'; } getchar(); } int res=0; for(int j=0;j<M;j++){ for(int k=0;k<N;k++){ if(visited[j][k]==0){ dfs(j,k); res++; } } } printf("%d\n",res); } return 0; }