• HDU 5652 India and China Origins 二分+并查集


    India and China Origins

    题目连接:

    http://acm.hdu.edu.cn/showproblem.php?pid=5652

    Description

    A long time ago there are no himalayas between India and China, the both cultures are frequently exchanged and are kept in sync at that time, but eventually himalayas rise up. With that at first the communation started to reduce and eventually died.

    Let's assume from my crude drawing that the only way to reaching from India to China or viceversa is through that grid, blue portion is the ocean and people haven't yet invented the ship. and the yellow portion is desert and has ghosts roaming around so people can't travel that way. and the black portions are the location which have mountains and white portions are plateau which are suitable for travelling. moutains are very big to get to the top, height of these mountains is infinite. So if there is mountain between two white portions you can't travel by climbing the mountain.
    And at each step people can go to 4 adjacent positions.

    Our archeologists have taken sample of each mountain and estimated at which point they rise up at that place. So given the times at which each mountains rised up you have to tell at which time the communication between India and China got completely cut off.

    Input

    There are multi test cases. the first line is a sinle integer T which represents the number of test cases.

    For each test case, the first line contains two space seperated integers N,M. next N lines consists of strings composed of 0,1 characters. 1 denoting that there's already a mountain at that place, 0 denoting the plateau. on N+2 line there will be an integer Q denoting the number of mountains that rised up in the order of times. Next Q lines contain 2 space seperated integers X,Y denoting that at ith year a mountain rised up at location X,Y.

    T≤10

    1≤N≤500

    1≤M≤500

    1≤Q≤N∗M

    0≤X<N

    0≤Y<M

    Output

    Single line at which year the communication got cut off.

    print -1 if these two countries still connected in the end.

    Hint:

    From the picture above, we can see that China and India have no communication since 4th year.

    Sample Input

    1
    4 6
    011010
    000010
    100001
    001000
    7
    0 3
    1 5
    1 3
    0 0
    1 2
    2 4
    2 1

    Sample Output

    4

    Hint

    题意

    给一个n*m的图

    然后1表示有障碍物,中国在最上面,印度在最下面

    然后有q个变化,每次变化都会有一个地方从0变成障碍物

    问你最早什么时候,中国和印度不相连了。

    题解:

    比较显然的就是二分+判断是否连通

    判断是否连通用并查集和bfs都可以

    反正都是O(n)嘛

    代码

    #include<stdio.h>
    #include<iostream>
    #include<math.h>
    #include<cstring>
    using namespace std;
    const int maxn = 505;
    int p[505*505],n,m,q;
    char s[maxn][maxn];
    int vis[maxn][maxn];
    pair<int,int>d[maxn*maxn];
    int dx[4]={1,-1,0,0};
    int dy[4]={0,0,1,-1};
    int fi(int x)
    {
        return p[x]==x?x:p[x]=fi(p[x]);
    }
    void uni(int p1,int q1)
    {
        int x=fi(p1);
        int y=fi(q1);
        if(x!=y)
            p[x]=y;
    }
    int check(int t)
    {
        for(int i=0;i<=n*m+1;i++)p[i]=i;
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
                if(s[i][j]=='1')vis[i][j]=1;
                else vis[i][j]=0;
        for(int i=1;i<=t;i++)
            vis[d[i].first][d[i].second]=1;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                for(int k=0;k<4;k++)
                {
                    if(vis[i][j]==1)continue;
                    int xx = i+dx[k];
                    int yy = j+dy[k];
                    if(yy<=0||yy>m)continue;
                    if(xx==0)uni((i-1)*m+j,0);
                    else if(xx==n+1)uni((i-1)*m+j,n*m+1);
                    else
                    {
                        if(vis[xx][yy]==1)continue;
                        uni((i-1)*m+j,(xx-1)*m+yy);
                    }
                }
            }
        }
        int x = fi(0);
        int y = fi(n*m+1);
        if(x==y)return 1;
        return 0;
    }
    void solve()
    {
        memset(vis,0,sizeof(vis));
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
            scanf("%s",s[i]+1);
        scanf("%d",&q);
        for(int i=1;i<=q;i++)
        {
            scanf("%d%d",&d[i].first,&d[i].second);
            d[i].first++,d[i].second++;
        }
    
        if(check(q)){
            printf("-1
    ");
            return;
        }
    
        int l = 0,r = q,ans = 0;
        while(l<=r)
        {
            int mid = (l+r)/2;
            if(!check(mid))r=mid-1,ans=mid;
            else l=mid+1;
        }
        printf("%d
    ",ans);
    }
    int main()
    {
        int t;scanf("%d",&t);
        while(t--)solve();
    }
  • 相关阅读:
    基于 Laravel 完整开源项目大全
    微信分享 php jssdk
    点点客投票post抓包
    php一些 get就能提交的漏洞
    彩票网的数据接口 分析
    PHP一些实际常用的代码
    ThinkCMF常用代码
    php解压zip 待优化
    Python调用pywin32模块 发送QQ消息,打印窗口标题,切换窗口等句柄
    浅谈数列分块问题
  • 原文地址:https://www.cnblogs.com/qscqesze/p/5324497.html
Copyright © 2020-2023  润新知