hdu5652

来源:互联网 发布:魔法王座神器升阶数据 编辑:程序博客网 时间:2024/05/16 03:57

India and China Origins

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


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

Sample Output
4
题意:印度和中国在很久以前是连在一起的,后来由于地壳运动,印度与中国边界处形成了一个个高山,假设边界是一个n*m的网格,0表示平原可以通过人,1表示山,不能通过,随着时间推移,一些0变成了1,问最早在什么时候中国与印度不再能便利交通了;
二分加bfs搜索,看我代码
#include <iostream>#include <string.h>#include <stdio.h>#include <queue>using namespace std;const int maxn=505*505;struct point{    int x,y;    point()    {        ;    }    point(int a,int b)    {        x=a;        y=b;    }} d[maxn];char  Map[505][505],str[505][505];bool vis[505][505];int n,m;int dx[4]= {1,0,-1,0};int dy[4]= {0,1,0,-1};int  bfs(int x,int y){    queue<point>que;    que.push(point(x,y));    while(!que.empty())    {        int nx,ny;        point now=que.front();        que.pop();        vis[now.x][now.y]=true;        if(now.x==n-1)            return 1;        for(int i=0; i<4; i++)        {            nx=now.x+dx[i];            ny=now.y+dy[i];            if(nx>=0&&nx<n&&ny>=0&&ny<m&&Map[nx][ny]=='0'&&!vis[nx][ny])            {                que.push(point(nx,ny));                vis[nx][ny]=true;            }        }    }    return 0;}int v[maxn];int main(){    int t;    scanf("%d",&t);    int k;    while(t--)    {        scanf("%d%d",&n,&m);        for(int i=0; i<n; i++)            scanf("%s",Map[i]);        memcpy(str,Map,sizeof(Map));        scanf("%d",&k);        d[0].x=n,d[0].y=m;        for(int i=1; i<=k; i++)            scanf("%d%d",&d[i].x,&d[i].y);        int l=0,r=k;        int mid;        memset(v,0,sizeof(v));        while(l<r)        {            mid=(l+r)/2;            if(v[mid])                break;            v[mid]=1;            memcpy(Map,str,sizeof(str));            for(int i=1;i<=mid;i++)            Map[d[i].x][d[i].y]='1';            int flag=0;            for(int j=0; j<m; j++)            {                if(Map[0][j]=='0')                {                    memset(vis,false,sizeof(vis));                    int ans=bfs(0,j);                    if(ans==1)                    {                        flag=1;                        break;                    }                }            }            if(flag)            {                l=mid+1;            }            else            {                r=mid;            }        }      memcpy(Map,str,sizeof(str));            for(int i=1;i<=k;i++)            Map[d[i].x][d[i].y]='1';            int g=0;            for(int j=0; j<m; j++)            {                if(Map[0][j]=='0')                {                    memset(vis,false,sizeof(vis));                    int ans=bfs(0,j);                    if(ans==1)                    {                        g=1;                        break;                    }                }            }        if(g==0)        printf("%d\n",l);        else        printf("-1\n");    }    return 0;}


1 0
原创粉丝点击