关闭

TOJ 4104 Farm Irrigation

标签: TOJacmit
252人阅读 评论(0) 收藏 举报

Deal details carefully.

The portal:http://acm.tju.edu.cn/toj/showp4104.html

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>

int N,M;
char Map[55][55];
int mat[55][55];
int bin[3000];
char Judge_Right[256][256];
char Judge_Down[256][256];

void Pre_deal(){
    memset(Judge_Right,0,sizeof(Judge_Right));
    memset(Judge_Down,0,sizeof(Judge_Down));
    for(int i='A';i<='Z';i++){
        if(i == 'B' || i == 'D' || i == 'F' || i == 'G' || i == 'I' || i == 'J' || i == 'K'){
            Judge_Right[i]['C'] = 1;Judge_Right[i]['F'] = 1;Judge_Right[i]['G'] = 1;Judge_Right[i]['A'] = 1;
            Judge_Right[i]['H'] = 1;Judge_Right[i]['I'] = 1;Judge_Right[i]['K'] = 1;
        }
    }
    for(int i='A';i<='Z';i++){
        if(i == 'C' || i == 'D' || i == 'E' || i == 'H' || i == 'I' || i == 'J' || i == 'K'){
            Judge_Down[i]['B'] = 1;Judge_Down[i]['E'] = 1;Judge_Down[i]['G'] = 1;Judge_Down[i]['A'] = 1;
            Judge_Down[i]['H'] = 1;Judge_Down[i]['J'] = 1;Judge_Down[i]['K'] = 1;
        }
    }
}

int findx(int x){
    if(bin[x] != x)
        bin[x] = findx(bin[x]);
    return bin[x];
}

void merge_(int x,int y){
    //printf("%d %d\n",x,y);
    int fx = findx(x);
    int fy = findx(y);
    //printf("%d %d\n",fx,fy);
    if(fx == fy) return ;
    bin[fx] = fy;
}

void Deal_with(){
    Pre_deal();
    while(scanf("%d %d",&N,&M)){
        if(N < 0 || M < 0) break;
        for(int i=0;i<N;i++){
            scanf("%s",Map[i]);
        }
        int num = 1;
        for(int i=1;i<=N;i++){
            for(int j=1;j<=M;j++){
                mat[i][j] = num ++;
            }
        }
        for(int i=1;i<=2500;i++){
            bin[i] = i;
        }
        for(int i=1;i<=N;i++){
            for(int j=1;j<=M;j++){
                //if(i + 1 > N || j + 1 > M) continue;
                if(j+1<=M)
                if(Judge_Right[Map[i-1][j-1]][Map[i-1][j]])
                    merge_(mat[i][j],mat[i][j+1]);
                if(i+1<=N)
                if(Judge_Down[Map[i-1][j-1]][Map[i][j-1]])
                    merge_(mat[i][j],mat[i+1][j]);
            }
        }
        int cnt = 0;
        for(int i=1;i<=mat[N][M];i++){
            if(bin[i] == i){
        //        printf("%d\n",i);
                cnt ++;
            }
        }
        //puts("");
        printf("%d\n",cnt);
    }
}

int main(void){
    //freopen("a.in","r",stdin);
    Deal_with();
    return 0;
}



0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:59284次
    • 积分:3592
    • 等级:
    • 排名:第9127名
    • 原创:315篇
    • 转载:3篇
    • 译文:0篇
    • 评论:4条
    文章分类
    最新评论