ECNUSUM4 J hdu5652India and China Origins

来源:互联网 发布:mysql注入式攻击 编辑:程序博客网 时间:2024/05/22 02:26

India and China Origins

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


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
4 601101000001010000100100070 31 51 30 01 22 42 1
 

Sample Output
4
训练赛第四场:题目是“一道图论”
然后。。搜了下题意。。大多数人都写的“并查集”
想了下我好像最近刚在看这个。于是打算做一做。
结果发现是个二维的并查集。。不会GG
然后发现可以用bfs做和二分(其实就是看了别人说的)
题意:给你一个图,上面的1表示山峰(即不能走),每一年都会新增一座山峰,问你第几年开始,从上端点不能达到下端点。
暴力计算,每增一座山就bfs一次肯定要超时。
所以要二分 一开始没理解二分的含义
二分是指:每次取中间的某一年,计算是否联通,如果联通,则答案一定在此年之前。否则 答案在年以后。
如果不能联通,则表明肯定有答案。将ans赋值,如果搜到最后还是联通的,则ans=-1(即初始化值)
bfs的过程是,每次将第一排中非山峰的加入queue,然后进行搜索。
orz并查集的方法还是以后再说吧。。太菜了
#include <iostream>
#include <cstdio>#include <cstdlib>#include <cstring>#include <string>#include <algorithm>#include <vector>#include <cmath>#include <queue>#define N 100010using namespace std;char Map[510][510];int dx[4]={1,-1,0,0};int dy[4]={0,0,1,-1};bool visit[510][510];struct Q{    int x,y;};Q q[510*510];int n,m;int bfs(){    memset(visit,0,sizeof(visit));    queue<Q> q;    Q a,next;    for(int i=0; i<m; i++)        if(Map[0][i]=='0')        {            a.x=0,a.y=i;            q.push(a);            visit[a.x][a.y]=1;        }//把顶部的非山峰点加入队列    while(!q.empty())    {        a=q.front();        q.pop();        if(a.x==n-1)return 1;        for(int ii=0;ii<4;ii++)            {                int X=dx[ii]+a.x,Y=dy[ii]+a.y;                if(X>=0&&X<n&&Y>=0&&Y<m&&visit[X][Y]==0&&Map[X][Y]=='0')                {                    next.x=X,next.y=Y;                    visit[X][Y]=1;                    q.push(next);                }            }    }    return 0;}int main(){    int cas;    scanf("%d",&cas);    while(cas--)    {        memset(Map,0,sizeof(Map));        //  int n,m;        scanf("%d%d",&n,&m);        for(int i=0; i<n; i++)            scanf("%s",Map[i]);        int t;       // Q q[510*510];        scanf("%d",&t);        for(int i=0; i<t; i++)            scanf("%d%d",&q[i].x,&q[i].y);        int l=0,r=t-1,mid,ans=-1;        while(l<=r)        {            mid=(l+r)/2;            for(int i=0; i<mid; i++)                Map[q[i].x][q[i].y]='1';            if(bfs())l=mid+1;            else            {                r=mid-1;                ans=mid;            }            for(int i=0; i<mid; i++)                Map[q[i].x][q[i].y]='0';        }        cout<<ans<<endl;    }    return 0;}