Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=5652
Test instructions: A n*m lattice point, 0 means to walk, 1 to block. Each node is four-headed. Start typing the initial state of the box, then enter the Q operation, indicating that the first (x, y) lattice from 0 to 1, ask you on the first few times can not be from the bottom line to the top row. China is above the top row, India is below the bottom line, and if it is eventually connected, output-1;
Idea: Direct offline reverse processing, use and check set maintenance;
#include <bits/stdc++.h>using namespacestd;#defineRep0 (I,L,R) for (int i = (l); i < (R); i++)#defineREP1 (I,L,R) for (int i = (l); I <= (r); i++)#defineRep_0 (i,r,l) for (int i = (r); i > (l); i--)#defineRep_1 (i,r,l) for (int i = (r); I >= (l); i--)#defineMS0 (a) memset (A,0,sizeof (a))#defineMS1 (a) memset (A,-1,sizeof (a))#defineMSi (a) memset (A,0x3f,sizeof (a))#defineINF 0x3f3f3f3f#defineLson L, M, RT << 1#defineRson m+1, R, RT << 1|1typedef pair<int,int>PII;#defineA First#defineB Second#defineMK Make_pairtypedef __int64 LL;TYPEDEF unsignedint UINT; template<typename t>voidRead1 (T &m) {T x=0, f=1;CharCh=GetChar (); while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();} while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} M= x*F;} Template<typename t>voidRead2 (T &a,t &b) {Read1 (a); Read1 (b);} Template<typename t>voidRead3 (T &a,t &b,t &c) {Read1 (a); Read1 (b); Read1 (c);} Template<typename t>void out(T a) {if(a>9) out(ATen); Putchar (A%Ten+'0');}intT,kase =1, i,j,k,n,m;Const intN =507;CharS[n][n];queue<PII>Pq;inlineBOOLCheckintNxintNY) { if(NX <0|| NX >= N | | NY <0|| NY >= m)return false; return true;}intdir[2][4] = {{0,1,0,-1},{1,0,-1,0}};intf[n*N];intFind (intA) {returna==f[a]?f[a]:f[a]=Find (F[a]);}void_union (intAintb) {intp = Find (a), q = find (b); f[p] =q;}intIdxintRintc) {returnr*m+C;}voidBFS (intRintc) {Pq.push ({r,c}); intD =Find (IDX (r,c)); while(!Pq.empty ()) {R= Pq.front (). A,c =Pq.front (). B;pq.pop (); for(inti =0; I <4; i++){ intx = r + dir[0][i], y = c + dir[1][i]; intID =Find (idx (x, y)); if(!check (x, y) | | s[x][y] = ='1'|| id = = d)Continue; F[id]=D; Pq.push ({x, y}); } }}intx[n*n],y[n*N];intMain () {//freopen ("Data.txt", "R", stdin); //freopen ("OUT.txt", "w", stdout);Read1 (T); while(t--) {read2 (n,m); intTot = nm; REP1 (i,0, tot+2) F[i] =i; Rep0 (i,0, N) scanf ("%s", S[i]); intQ; Read1 (Q); REP1 (i,1, Q) {read2 (x[i],y[i]); S[x[i]][y[i]]='1'; } rep0 (I,0, N) rep0 (J,0, m)if(S[i][j] = ='0'){ intindex =idx (I,J); if(F[index] = =index) {BFS (I,J); }} rep0 (I,0, m) _union (i,tot+1); Rep0 (i,0, m) _union (IDX (N-1, i), tot+2); if(f[tot+1] = = f[tot+2]) {puts ("-1"); Continue; } rep_1 (I,q,1) {S[x[i]][y[i]]='0'; BFS (X[i],y[i]); if(Find (tot+1) = = Find (tot+2)){ out(i); Puts (""); Break; } } } return 0;}
HDU 5652 India and China origins and check set + reverse order