HDU 5652 India and China Origins and collection

Source: Internet
Author: User

India and China origins

problem DescriptionA long time ago There is no Himalayas between India and China, the both cultures is frequently exchanged and is kept in Sync at this time, but eventually Himalayas rise up. With, at first the communation started to reduce and eventually died.



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

Our archeologists has taken sample of each mountain and estimated at which point they rise up at the place. So given, the times at which each mountains rised up, which has the to-tell at which time the communication between India and Chi Na got completely cut off.

InputThere is multi test cases. The first line is a Sinle integerTwhich represents the number of test cases.

For each test case, the first line contains the seperated integersN,M . NextNLines consists of strings composed of0,1 Characters.1Denoting that there's already a mountain at this place,0Denoting the plateau. OnN+2 Line there'll be is an integerQDenoting the number of mountains that rised the order of the Times. NextQLines contain2Space seperated integersX,Y Denoting of ith year a mountain risedX,Y .

T≤ten

1≤N≤

1≤M≤

1≤Q≤N∗M

0≤X<N

0≤Y<M

OutputSingle, which year the communication got cut off.

Print-1 if these, countries still connected in the end.

Hint:



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

Sample Input14 601101000001010000100100070 31 51 30 01 22 42 1

Sample Output4

Exercises

This is a question of connectivity. You will find that it is easy to use and check the set if you look at all the actions in reverse order. First, all the peaks are added to the diagram, and then the reverse processing of each operation: on a certain operation, in the diagram to delete the location of the mountain, and then determine whether two points are connected, once connected to get the results. There is a need to create a corresponding node for China and India.

#pragmaComment (linker, "/stack:10240000,10240000")#include<stdio.h>#defineN 505#defineM 250005using namespacestd;intF[M],X[M],Y[M];CharA[n][n];intM,n,q,st,ed,i,j,t,now;int Get(intu) {returnf[u]==u?u:f[u]=Get(F[u]);}voidReadint&x) {    CharCh=getchar (); x=0;  for(;ch<'0'; ch=GetChar ());  for(; ch>='0'; Ch=getchar ()) x=x*Ten+ch-'0';} InlineintID (intXintY) {return(X-1) *m+y;} InlinevoidUnion (intXintY) {f[Get(x)] =Get(y);} InlinevoidWorkintIintj) {    if(i==1) Union (ID (i,j), ST);Else if(a[i-1][j]=='0') Union (ID (i-1, J), ID (i,j)); if(j>1&&a[i][j-1]=='0') Union (ID (I,J), ID (i,j-1)); if(j<m&&a[i][j+1]=='0') Union (ID (I,J), ID (i,j+1)); if(i==n) Union (ID (I,j), ed);Else if(a[i+1][j]=='0') Union (ID (i+1, J), ID (I,j));}intMain () {read (T);  while(t--) {read (n); read (m);  for(i=1; i<=n;i++) scanf ("%s", a[i]+1);        Read (Q);  for(i=1; i<=q;i++) read (X[i]), read (Y[i]), a[++x[i]][++y[i]]='1';  for(i=1; i<=n*m+2; i++) f[i]=i; St=n*m+1; ed=st+1;  for(i=1; i<=n;i++)             for(j=1; j<=m;j++)                if(a[i][j]=='0') work (I,J); if(Get(ST) = =Get(ed)) {Puts ("-1");Continue;}  for(now=Q;now;) {A[x[now]][y[now]]='0';            Work (X[now],y[now]); if(Get(ST) = =Get(ed)) Break; now--; }printf ("%d\n", now); }}

HDU 5652 India and China Origins and collection

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.