#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#define N 20510
#define FOR(i,n) for(int i=0;i<n;i++)
#define INF 0x7fffffff
using namespace std;
struct Node
{
int v,next;
}edge[N];
int head[N];
char tm[100][100], vis[N], dir[20][4];
int di[4][2]={-1,0,0,1,1,0,0,-1};
void init()
{
FOR(i,16)FOR(j,4) dir[i][j]=1;
dir[0][2]=dir[0][1]=0;
dir[1][2]=dir[1][3]=0;
dir[2][0]=dir[2][1]=0;
dir[3][0]=dir[3][3]=0;
dir[4][3]=dir[4][1]=0;
dir[5][2]=dir[5][0]=0;
dir[6][2] = dir[7][1]=0;
dir[8][0] = dir[9][3]=0;
}
bool is_link( char a, char b, int d )
{
int x = a-'A';
int y = b-'A';
if( dir[y][(d+2)%4] == 1 && dir[x][d] == 1 ) return true;
return false;
}
void add_edge( int u, int v, int &num )
{
edge[num].v = v;
edge[num].next = head[u];
head[u] = num ++;
edge[num].v = u;
edge[num].next = head[v];
head[v] = num ++;
}
void get_map( int m, int n )
{
FOR(i,m) scanf("%s",tm[i]);
memset(head,-1,sizeof(head));
int num = 0;
FOR(i,m)FOR(j,n)
{
int u = i*n+j;
FOR(k,4)
{
int x = i+di[k][0];
int y = j+di[k][1];
if( x>=0&&y>=0&&x<m&&y<n &&is_link(tm[i][j],tm[x][y],k) )
add_edge(u,x*n+y,num);
}
}
}
void dfs( int u )
{
vis[u] = 1;
for( int p = head[u]; p!=-1; p=edge[p].next )
if( !vis[edge[p].v] )
dfs(edge[p].v);
}
int main()
{
int n,m,cas, w,u,v,k;
char str[5];
init();
while( ~scanf("%d%d",&m,&n) )
{
if( m<=-1&&n<=-1)break;
get_map(m,n);
memset(vis,0,sizeof(vis));
int ans = 0;
FOR(i,n*m) {
if( !vis[i] )
{
dfs(i);
ans++;
}
}
printf("%d\n",ans);
}
return 0;
}
hdu 1198
最新推荐文章于 2024-08-29 22:25:44 发布