hdu1198Farm Irrigation

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1198
两种方法:dfs和并查集
我是在最小生成树的分类做的这个题,说好的最小生成树呢。。。

dfs实现:
   初始化每个板块四个方向的连通性,然后dfs就可以了,搜索的时候注意越界,重复访问,下一个板块的连通性问题。
#include 
#include 

char s[100][100];
int n,m;
struct Node
{
    int up;
    int down;
    int left;
    int right;
} a[30];
int v[100][100];
int dx[] = {-1,0,1,0};
int dy[] = {0,1,0,-1};

void init()

{
    memset(a,0,sizeof(a));
    a[0].up = a[0].left = 1;
    a[1].up = a[1].right = 1;
    a[2].down = a[2].left = 1;
    a[3].down = a[3].right = 1;
    a[4].up = a[4].down = 1;
    a[5].left = a[5].right = 1;
    a[6].up = a[6].left = a[6].right = 1;
    a[7].left = a[7].up = a[7].down = 1;
    a[8].left = a[8].right = a[8].down = 1;
    a[9].up = a[9].down  = a[9].right = 1;
    a[10].up = a[10].down = a[10].left = a[10].right = 1;
}

void dfs(int x,int y)

{
    //printf("%d %d\n",x,y);
    v[x][y] = 1;
    if(a[s[x][y] - 'A'].up)
    {
        if(x - 1>= 0 && !v[x - 1][y] && a[s[x - 1][y] - 'A'].down)
            dfs(x - 1,y);
    }
    if(a[s[x][y] - 'A'].right)
    {
        if(y + 1 < m && !v[x][y + 1] && a[s[x][y + 1] - 'A'].left)
            dfs(x,y + 1);
    }
    if(a[s[x][y] - 'A'].down)
    {
        if(x + 1 < n && !v[x + 1][y] && a[s[x + 1][y] - 'A'].up)
            dfs(x + 1,y);
    }
    if(a[s[x][y] - 'A'].left)
        if(y - 1 >= 0 && !v[x][y - 1] && a[s[x][y - 1] - 'A'].right)
            dfs(x,y - 1);
}

int main()

{
    init();
    while(~scanf("%d%d",&n,&m))
    {
        if(n < 0 || m < 0)
        {
            break;
        }
        for(int i = 0; i < n; ++i)
            scanf("%s",s[i]);
        memset(v,0,sizeof(v));
        int cnt  = 1;
        for(int i = 0 ; i < n; ++i)
            for(int j = 0; j < m; ++j)
                if(!v[i][j])
                {
                     //printf("%d %d\n",i,j);
                     dfs(i,j);
                     cnt++;
                }
        printf("%d\n",cnt - 1);
    }
}
并查集做法:

#include 
#include 

char a[20][5] = {"1001","1100","0011","0110","1010","0101","1101","1011","0111","1110","1111"};

int n,m;
char s[100][100];
int p[300][300];

int find(int x)

{
    return p[x / m][x % m] == x ? x : p[x / m][x % m] = find(p[x / m][x % m]);
}
void Union(int x,int y)

{
    x = find(x);
    y = find(y);
    if(x != y)
        p[y / m][y % m] = x;
}
void judge(int i,int j)

{
    if(j > 0 && a[s[i][j] - 'A'][3] == '1' && a[s[i][j - 1] - 'A'][1] == '1')
        Union(i * m + j,i * m + j - 1);
    if(i > 0 && a[s[i][j] - 'A'][0] == '1' && a[s[i - 1][j] - 'A'][2] == '1')
        Union(i * m + j,(i - 1) * m + j);
}
int main()

{
    while(~scanf("%d%d",&n,&m))
    {
        memset(p,0,sizeof(p));
        if(n < 0 || m < 0)
            break;
        for(int i = 0;i < n;++i)
            scanf("%s",s[i]);
        for(int i = 0;i < n;++i)
            for(int j = 0;j < m;++j)
                 p[i][j] = i * m + j;
        for(int i = 0;i < n;++i)
            for(int j = 0;j < m;++j)
                judge(i,j);
        int cnt = 0;
        for(int i = 0;i < n;++i)
            for(int j = 0;j < m;++j)
                if(p[i][j] == i * m + j)
                   ++cnt;
        printf("%d\n",cnt);
    }
    return 0;
}


你可能感兴趣的:(dfs,并查集)