TOJ 4104 Farm Irrigation

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;
}



阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/NMfloat/article/details/46820403
文章标签: TOJ acm it
上一篇TOJ 4107 A simple problem
下一篇TOJ 4108 Hearthstone
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭