zoukankan      html  css  js  c++  java
  • hdu 5652 India and China Origins

    India and China Origins

    Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
    Total Submission(s): 901    Accepted Submission(s): 309


    Problem 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.

    T10

    1N500

    1M500

    1QNM

    0X<N

    0Y<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
     

    题意    一開始有一个地图  白格能走  黑格不能走  仅仅能走到相邻格子  起点是最上面一层任意的白格  终点是底层的白格

       之后的Q年中  每年都会在Xq,Yq的格子中出现一座山  该格子将不能通行  问第几年时  没有路径从上走到下


    一道简单的搜索题。先记录一天从上到下的路径。若生成的山在记录的路径上,就又一次搜索路径,知道没有路径或者没有山生成。

    不必要每次生成山就去搜索一次路径


    #include<algorithm>
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<queue>
    using namespace std;
    int n,m;
    char Map[505][505];
    bool vis[505][505];  //记录一条从上走到下的路径  经过的点是true 
    int dir[4][2]= {0,1,1,0,0,-1,-1,0};
    int rr;
    struct node
    {
        int x,y;
        bool cheak()
        {
            if(x>=0&&x<n&&y>=0&&y<m)
                return true ;
            return false ;
        }
    };
    
    bool bfs()  //广搜路径
    {
        //cout<<rr<<"*"<<endl;
        queue<node>q;
        int mat[505][505];
        memset(mat,-1,sizeof(mat));
        memset(vis,false ,sizeof(vis));
        node st,ed;
        for(int i=0; i<m; i++)
        {
            if(Map[0][i]=='0')
            {
                st.x=0;
                st.y=i;
                mat[st.x][st.y]=-2;
                q.push(st);
            }
        }
        while(q.size())
        {
            st=q.front();
            if(st.x==n-1)  //找到走到最以下的路径了  标记路径
            {
                int ans=10;
                vis[st.x][st.y]=true ;
                while(mat[st.x][st.y]!=-2)
                {
                    ed.x=mat[st.x][st.y]/m;
                    ed.y=mat[st.x][st.y]%m;
                    st=ed;
                    vis[st.x][st.y]=true ;
                }
                return true ;
            }
            q.pop();
            for(int i=0; i<4; i++)
            {
                ed=st;
                ed.x+=dir[i][0];
                ed.y+=dir[i][1];
                if(ed.cheak()&&Map[ed.x][ed.y]=='0'&&mat[ed.x][ed.y]==-1)
                {
                    mat[ed.x][ed.y]=st.x*m+st.y;  //记录前驱节点
                    q.push(ed);
                }
            }
        }
        return false ;
    }
    
    int main()
    {
        int t;
        scanf("%d",&t);
        while(t--)
        {
            scanf("%d%d",&n,&m);
            for(int i=0; i<n; i++)
                scanf("%s",Map[i]);
            rr=0;
            memset(vis,false ,sizeof(vis));
            bool flag=bfs();
            int T,xx,yy;
            int ans=-1;
            if(!flag) ans=0;  //一開始就堵住的情况
            scanf("%d",&T);
            for(int i=1;i<=T;i++)
            {
                scanf("%d%d",&xx,&yy);
                Map[xx][yy]='1';
                if(!flag) continue ;  //已经被堵住
                if(vis[xx][yy])  //假设新生成的山在我的路径上  就要又一次搜索路径
                flag=bfs();
                if(!flag) ans=i;  //找不到路径
            }
            printf("%d
    ",ans);
        }
        return 0;
    }
    

      


  • 相关阅读:
    那一定都是你的错!- 一次FastDFS并发问题的排查经历
    程序员和烟民
    Spring Boot(十六):使用 Jenkins 部署 Spring Boot
    阿里Dubbo疯狂更新,关Spring Cloud什么事?
    从架构演进的角度聊聊Spring Cloud都做了些什么?
    FastDFS 集群 安装 配置
    中小型互联网公司微服务实践-经验和教训
    jvm系列(十一):JVM演讲PPT分享
    jvm系列(十):如何优化Java GC「译」
    Spring Boot (十五): Spring Boot + Jpa + Thymeleaf 增删改查示例
  • 原文地址:https://www.cnblogs.com/yfceshi/p/7337896.html
Copyright © 2011-2022 走看看