zoukankan      html  css  js  c++  java
  • hdu 1198 dfs||并查集求连通分量个数,关键建图

    题意:

    有A~K共9种类型的地步,每耕田上方的沟渠是不合的(它们连接的出口不合)。然后由这些类型的田构成一个n*m大小的更大的田,因为相邻两个地步的沟渠可能可以相通(有接口),所以只要相通的那些地步只须要一个水源就可以了。求起码须要几许个水源。

    解析:

    可用bfs,dfs做,不过用并查集更便利点。 用并查集只须要断定一个田四周的四个田,若是有接口,那么就把这两个田归并成一棵树,最后断定几棵树即可。

    分析:实在不会dfs啊。嗨,真心不会。。。。就是没感觉。好吧,借口太多了,掌嘴。

    View Code
    // I'm lanjiangzhou
    //C
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    #include <math.h>
    #include <time.h>
    //C++
    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <cstring>
    #include <cctype>
    #include <stack>
    #include <string>
    #include <list>
    #include <queue>
    #include <map>
    #include <vector>
    #include <deque>
    #include <set>
    using namespace std;
    
    //*************************OUTPUT*************************
    #ifdef WIN32
    #define INT64 "%I64d"
    #define UINT64 "%I64u"
    #else
    #define INT64 "%lld"
    #define UINT64 "%llu"
    #endif
    
    //**************************CONSTANT***********************
    #define INF 0x3f3f3f3f
    // aply for the memory of the stack
    //#pragma comment (linker, "/STACK:1024000000,1024000000")
    //end
    
    const int maxn = 100+10;
    int n,m;
    //将正方形地块上的水管按照顺时针的方向标记,有为1,没有为0
    int a[12][4]={{1,0,0,1},{1,1,0,0},{0,0,1,1},{0,1,1,0},{1,0,1,0},{0,1,0,1},{1,1,0,1},{1,0,1,1},{0,1,1,1},{1,1,1,0},{1,1,1,1}};
    int maps[maxn][maxn];
    bool vis[maxn][maxn];//访问标记
    
    void dfs(int x,int y){
        vis[x][y]=1;
        for(int z=0;z<4;z++){
            //
            if(z==0&&a[maps[x][y]][0]&&x>=1&&a[maps[x-1][y]][2]&&!vis[x-1][y])
                dfs(x-1,y);
            //
            else if(z==1&&a[maps[x][y]][1]&&y+1<m&&a[maps[x][y+1]][3]&&!vis[x][y+1])
                dfs(x,y+1);
            //
            else if(z==2&&a[maps[x][y]][2]&&x+1<n&&a[maps[x+1][y]][0]&&!vis[x+1][y])
                dfs(x+1,y);
            //
            else if(z==3&&a[maps[x][y]][3]&&y>=1&&a[maps[x][y-1]][1]&&!vis[x][y-1])
                dfs(x,y-1);
        }
    }
    
    int main(){
        char ch[maxn];
        while(scanf("%d%d",&n,&m)!=EOF){
            if(n==-1&&m==-1)  break;
            memset(maps,0,sizeof(maps));
            for(int i=0;i<n;i++){
                scanf("%s",ch);
                for(int j=0;j<m;j++){
                    maps[i][j]=ch[j]-'A';
                }
            }
            memset(vis,0,sizeof(vis));
            int sum=0;
            for(int i=0;i<n;i++){
                for(int j=0;j<m;j++){
                    if(vis[i][j]) continue;
                    dfs(i,j);
                    sum++;
                }
            }
            printf("%d\n",sum);
        }
        return 0;
    }

    下面是并查集做的,感觉和上面一样http://www.cesclub.com/bw/jishuzhongxin/bianchengyuyan/2012/0908/39881.html

    http://blog.csdn.net/fanxing1/article/details/5746377

    写的都很好,嗨,不会dfs只能求人,不会建图,怎么办搜呗。

  • 相关阅读:
    UE4 Hello World 创建第一个UE4工程
    集团企业数据信息系统建设方案
    Ubuntu_ROS中应用kinect v2笔记
    电力企业计量生产运行系统总体解决方案
    电力企业信息化建设解决方案之计量生产分析系统
    BQ24296充电管理芯片使用过程中的注意事项
    微信测试号开发之四 获取access_token和jsapi_ticket
    微信测试号开发之五 自定义菜单
    微信测试号开发之六 图灵自动回复文本消息
    微信测试号开发之七 获取用户地理位置
  • 原文地址:https://www.cnblogs.com/lanjiangzhou/p/2997587.html
Copyright © 2011-2022 走看看