hdu 1198

//很有意思的一个题目  判断连通则并集

#include<iostream>
#include<cstdio>
#include<string>
using namespace std;
struct node{
 int x;
 int y;
};
node p[52][52];
int deep[52][52];
int dir[4][2] = {{-1,0},{1, 0},{0,-1},{0,1}};
bool hash[52][52];
char map[52][52];
inline void make_set(int n,int m)
{
 int i,j;
 for(i = 0; i <= n; i++)
 for(j = 0; j <= m; j++)
 {  
  p[i][j].x = i;
  p[i][j].y = j;
  deep[i][j] = 1;
  hash[i][j] = true;
 }
}
inline node find(node x)
{
 node r,i;
 r = x;
 while(p[r.x][r.y].x != r.x || p[r.x][r.y].y != r.y)
 {
  i = r;
  r = p[r.x][r.y];
  p[i.x][i.y] = p[r.x][r.y];
 }
 return r;
}
inline void merge(node a,node b)
{
 node fa = find(a);
 node fb = find(b);
 if(fa.x != fb.x || fb.y != fa.y)
 {
  if(deep[fa.x][fa.y] > deep[fb.x][fb.y])
  {
   p[fb.x][fb.y] = fa;
  }
  else
  {
   p[fa.x][fa.y] = fb;
   if(deep[fa.x][fa.y] == deep[fb.x][fb.y])
   {
    deep[fb.x][fb.y]++;
   }
  }
 }
}
bool isralate(char c,int i)
{
 if(c == 'A')
 {
  if(i == 0 || i == 2)
  return true;
 }
 else if(c == 'B')
 {
  if(i == 0 || i == 3)
  return true;
 }
 else if(c == 'C')
 {
  if(i == 1 || i == 2)
  return true;
 }
 else if(c == 'D')
 {
  if(i == 1 || i == 3)
  return true;
 }
 else if(c == 'E')
 {
  if(i == 0 || i == 1)
  return true;
 }
 else if(c == 'F')
 {
  if(i == 2 || i == 3)
  return true;
 }
 else if(c == 'G')
 {
  if(i == 0 || i == 3 || i == 2)
  return true;
 }
 else if(c == 'H')
 {
  if(i == 0 || i == 1 || i == 2)
  return true;
 }
 else if(c == 'I')
 {
  if(i == 1 || i == 3 || i == 2)
  return true;
 }
 else if(c == 'J')
 {
  if(i == 0 || i == 3 || i == 1)
  return true;
 }
 else if(c == 'K')
 {
  return true;
 }
 return false;
}
void judge(int n,int m)
{
 node N,P;
 int i,j,k;
 for(k = 0; k < n; k++)
 for(j = 0; j < m; j++)
 {
  N.x = k;
  N.y = j;
  for(i = 0; i < 4; i++)
  {
   P.x = N.x + dir[i][0];
   P.y = N.y + dir[i][1];
   if(P.x >= n || P.y >= m || P.x < 0 || P.y < 0 || hash[P.x][P.y] == false)continue;
   if(i == 0)
   {
    if(isralate(map[P.x][P.y],1) && isralate(map[N.x][N.y],i))
    {
     merge(N,P);
    }
   }
   else if(i == 1)
   {
    if(isralate(map[P.x][P.y],0) && isralate(map[N.x][N.y],i))
    {
     merge(N,P);
    }
   }
   else if(i == 2)
   {
    if(isralate(map[P.x][P.y],3) && isralate(map[N.x][N.y],i))
    {
     merge(N,P);
    }
   }
   else if(i == 3)
   {
    if(isralate(map[P.x][P.y],2) && isralate(map[N.x][N.y],i))
    {
     merge(N,P);
    }
   }
   hash[N.x][N.y] = false;
  }
 }
}
int main()
{
 int n,m;
 int i,j;
 int count;
 while(scanf("%d%d",&n,&m) != EOF && (n >= 0 && m >= 0))
 {
  make_set(n,m);
  count = 0;
  for(i = 0; i < n; i++)
  {
   scanf("%s",&map[i]);
  }
  judge(n,m);
  for(i = 0; i < n; i++)
  for(j = 0; j < m; j++)
  {
   if(p[i][j].x == i && p[i][j].y == j)
   {
    count++;
   }
  }
  printf("%d/n",count);
 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值