hdu 1198 Farm Irrigation ( 打表+并查集)

来源:互联网 发布:古装电影推荐 知乎 编辑:程序博客网 时间:2024/05/22 03:24

Farm Irrigation

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6432    Accepted Submission(s): 2787


Problem Description
Benny has a spacious farm land to irrigate. The farm land is a rectangle, and is divided into a lot of samll squares. Water pipes are placed in these squares. Different square has a different type of pipe. There are 11 types of pipes, which is marked from A to K, as Figure 1 shows.


Figure 1


Benny has a map of his farm, which is an array of marks denoting the distribution of water pipes over the whole farm. For example, if he has a map

ADC
FJK
IHE

then the water pipes are distributed like


Figure 2


Several wellsprings are found in the center of some squares, so water can flow along the pipes from one square to another. If water flow crosses one square, the whole farm land in this square is irrigated and will have a good harvest in autumn.

Now Benny wants to know at least how many wellsprings should be found to have the whole farm land irrigated. Can you help him?

Note: In the above example, at least 3 wellsprings are needed, as those red points in Figure 2 show.
 

Input
There are several test cases! In each test case, the first line contains 2 integers M and N, then M lines follow. In each of these lines, there are N characters, in the range of 'A' to 'K', denoting the type of water pipe over the corresponding square. A negative M or N denotes the end of input, else you can assume 1 <= M, N <= 50.
 

Output
For each test case, output in one line the least number of wellsprings needed.
 

Sample Input
2 2DKHF3 3ADCFJKIHE-1 -1
 

Sample Output
23
 

Author
ZHENG, Lu
 

Source
Zhejiang University Local Contest 2005
题目分析:并查集裸题,就是模拟的部分麻烦一点
 
#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>#define MAX 600using namespace std;bool connect[15][7];void init ( ){    // 1 for up , 2 for riht , 3 for down , 4 for left     memset ( connect , false , sizeof ( connect ) );    connect[1][1] = connect[2][1] = connect[5][1] = connect[7][1] = 1;    connect[8][1] = connect[10][1] = connect[11][1] = 1;    connect[2][2] = connect[4][2] = connect[6][2] = connect[7][2] = 1;    connect[9][2] = connect[10][2] = connect[11][2] = 1;    connect[3][3] = connect[4][3] = connect[5][3] = connect[8][3] = 1;    connect[9][3] = connect[10][3] = connect[11][3] = 1;    connect[1][4] = connect[3][4] = connect[6][4] = connect[7][4] = 1;    connect[8][4] = connect[9][4] = connect[11][4] = 1;}int dx[]={0,-1,0,1,0};int dy[]={0,0,1,0,-1};int n,m;int mp[MAX][MAX];char s[107];int get_id ( int x , int y ){    return (x-1)*m+y;}int fa[MAX*MAX];bool used[MAX*MAX];void init2 ( ){    for ( int i = 1 ; i < MAX*MAX ; i++ )        fa[i] = i;}int find ( int x ){    return x == fa[x] ? x : fa[x] = find(fa[x]);}void _union ( int x , int y ){    x = find(x);    y = find(y);    fa[x] = y;}int main ( ){    init( );    while ( ~scanf ( "%d%d" , &n , &m ) )    {        init2();        memset ( mp , -1 , sizeof ( mp ) );        if ( n == -1 && m == -1 ) break;        for ( int i = 1 ; i <= n ; i++ )        {            scanf ( "%s" , s+1 );            for ( int j = 1 ; j <= m ; j++ )                mp[i][j] = s[j]-64;        }        for ( int i = 1 ; i <= n ; i++ )            for ( int j = 1 ; j <= m ; j++ )                for ( int k = 1 ; k < 5 ; k++ )                {                    int tx = i+dx[k] , ty = j+dy[k];                    int type2 = mp[tx][ty];                    if ( type2 == -1 ) continue;                    int type1 = mp[i][j];                    if ( !connect[type1][k] ) continue;                    int k1;                    if ( k == 1 ) k1 = 3;                    if ( k == 2 ) k1 = 4;                    if ( k == 3 ) k1 = 1;                    if ( k == 4 ) k1 = 2;                    if ( connect[type2][k1] )                        _union ( get_id(i,j) , get_id(tx,ty) );                }        memset ( used , 0 , sizeof ( used ) );        int ans = 0;        for ( int i = 1 ; i <= n ; i++ )            for ( int j = 1 ; j <= m ; j++ )            {                int id = find( get_id(i,j) );                if ( used[id] ) continue;                used[id] = true;                ans++;            }        printf ( "%d\n" , ans );    }}


0 0