TOJ 4104 Farm Irrigation

原创 2015年07月09日 18:31:52

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



版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

(hdu step 5.1.4)Farm Irrigation(在两个节点合并有限制条件的情况下,求集合的个数)

题目:Farm IrrigationTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To...

【并查集】Farm Irrigation

Farm Irrigation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)...
  • ACM_Ted
  • ACM_Ted
  • 2012年04月12日 13:18
  • 679

HDU Farm Irrigation

Benny has a spacious farm land to irrigate. The farm land is a rectangle, and is divided into a lot ...

HDU 1198 Farm Irrigation 并查集

自己思路和答案思路一样这种事情最开心啦~其实不看答案自己应该也能改对 又是把行列搞反了 #include #include #include using namespace std; bool t...

L - Farm Irrigation

Description Benny has a spacious farm land to irrigate. The farm land is a rectangle, and is ...

HDU 1198 Farm Irrigation(二维转化一维)

Problem Description Benny has a spacious farm land to irrigate. The farm land is a rectangle, and i...

HDOJ 1198 Farm Irrigation(并查集)

Farm Irrigation Problem Description Benny has a spacious farm land to irrigate. The farm land i...

HDUOJ 1198 Farm Irrigation(并查集)

Farm Irrigation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)...
  • cs_zlg
  • cs_zlg
  • 2012年04月11日 17:48
  • 738

ZOJ 2412 Farm Irrigation

转载请注明出处:http://blog.csdn.net/a1dark 分析:蛋蛋疼、跟上一题一样、图的DFS遍历、不过这里需要预处理一下字符、然后再DFS、 #include #include ...

【HDU 1198】Farm Irrigation(字符串拼接/类油田问题/DFS)

Farm IrrigationTime HDU 1198 主要用深度优先搜索和拼接字符串来完成题目要求
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:TOJ 4104 Farm Irrigation
举报原因:
原因补充:

(最多只允许输入30个字)