HDU——1198

#include <cstdio>
#include <iostream>
using namespace std;


const int maxn = 50;
int father[maxn * maxn + 100];
char mat[maxn + 5][maxn + 5];
int n, m;


int get_father(int a) {
    //return father[a] == -1 ? (father[a] = a) : (father[a] = get_father(a));
	return father[a] == a ? a : (father[a] = get_father(father[a]));
}


void get_union(int a, int b) {
    int c = get_father(a);
    int d = get_father(b);
    father[c] = d;
}


bool ok_up(char a, char b) {//a在下,b在上
    if(a == 'A' || a == 'B' || a == 'E' || a == 'G' || a == 'H' || a == 'J' || a == 'K') {
        if(b == 'C' || b == 'D' || b == 'E' || b == 'H' || b == 'I' || b == 'J' || b == 'K') return true;
        else return false;
    }
    else return false;
}


bool ok_left(char a, char b) {//a在右,b在左
    if(a == 'A' || a == 'C' || a == 'F' || a == 'G' || a == 'H' || a == 'I' || a == 'K') {
        if(b == 'B' || b == 'D' || b == 'F' || b == 'G' || b == 'I' || b == 'J' || b == 'K') return true;
        else return false;
    }
    else return false;
}


void con_up(int a, int b, int c, int d) {
    if(ok_up(mat[a][b], mat[c][d])) get_union( a * m + b, c *m + d);
}


void con_left(int a, int b, int c, int d) {
    if(ok_left(mat[a][b], mat[c][d])) get_union( a * m + b, c * m + d);
}




void judge(int a, int b) {
    if(a > 0) con_up(a, b, a - 1, b);
    if(b > 0) con_left(a, b, a, b - 1);
}


int main() {
    while(~scanf("%d%d", &n, &m)) {
		if(n == -1 && m == -1) return 0;
        //memset(father, -1, sizeof(father));
		for(int i = 0; i < n * m + 10; i++) father[i] = i;
        int cnt = 0;
        for(int i = 0; i < n; i++) {
            getchar();
            for(int j = 0; j < m; j++) {
                scanf("%c", &mat[i][j]);
                judge(i, j);
                }
        }
        //cout << "here" << endl;
        for(int i = 0; i < n; i++) for(int j = 0; j < m; j++) { if(father[i * m + j] == i * m + j || father[i * m + j] == -1) cnt++;}
        printf("%d\n", cnt);
    }
    return 0;
}

AC了一道并查集,感觉挺简单的~~~吐舌头

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值