http://hihocoder.com/problemset/problem/1291
A few days ago more busy, this time to make up for the final Microsoft written test, this is the fourth question of the Microsoft written test, too few people, I was debugging B, there is no time to see this problem. But played before a bestcoder should be a bit of thinking, although the problem is two-dimensional BC, the problem is three-dimensional, but the idea should be the same, yes, is offline add and check set.
Is coming to consider when, found the first request adjacent block is good processing, but the second request can reach (1000, 1000, 1000) This condition seems to be more difficult to judge, at that time the title of the BC according to test instructions is can be binary plus search, but the condition of the problem is not two points.
Offline and check set words, better think (if done BC that question), is the first count on all blocks, search the full map, the use of non-block and check set Unicom. Then split the block upside down to see if the block to be dismantled and the boundary (where the boundary is taken (0, 0, 1) is non-block (with a common root), and then the place where the block is removed and the surrounding non-block is connected. Until a block in one place is not connected to the boundary, it is no. Otherwise, the last is yes.
Locally with DFS explosion, the BFS should be resolved. But directly hand in, the evaluation machine with DFS will not explode stack ...
Code:
#include <iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<queue>#include<map>#include<Set>#include<string>#include<vector>using namespacestd;Const intMAXN =100005;Const intMaxX =103;intufs[ the* the* the];intN, X[maxn], Y[MAXN], Z[MAXN];intXx[] = {-1,1,0,0,0,0};intYy[] = {0,0, -1,1,0,0};intZz[] = {0,0,0,0, -1,1};intFindRoot (intx) { if(Ufs[x] = = x)returnx; intFA =FindRoot (ufs[x]); UFS[X]=FA; returnFA;}voidMergeufs (intXinty) { intFX, FY; FX=findRoot (x); FY=findRoot (y); UFS[FX]=fy;}BOOLInsameufs (intXinty) { intFX, FY; FX=findRoot (x); FY=findRoot (y); if(FX = = FY)return true; Else return false;} InlineintHash (intXintYintz) { returnx*maxx*maxx+y*maxx+Z;}voidDfsintXintYintz) { intt =Hash (x, Y, z), TT; if(Ufs[t] = =-2|| UFS[T]! =-1)return; if(Ufs[t] = =-1) Ufs[t] =T; for(inti =0; I <6; ++i) {if(X+xx[i] <0|| Y+yy[i] <0|| Z+zz[i] <=0)Continue; if(X+xx[i] >= MaxX | | y+yy[i] >= MaxX | | z+zz[i] >= maxX)Continue; TT= Hash (X+xx[i], y+yy[i], z+Zz[i]); if(UFS[TT]! =-1)Continue; DFS (x+xx[i], Y+yy[i], z+Zz[i]); Mergeufs (t, TT); }}BOOLJudgeintXintYintz) { BOOLFlag =false; intT, TT, to; for(inti =0; I <6; ++i) {TT= Hash (X+xx[i], y+yy[i], z+Zz[i]); if(Ufs[tt] = =-2) flag =true; } if(!flag)return false; Flag=false; T=Hash (x, y, z); to= Hash (0,0,1); Ufs[t]=T; for(inti =0; I <6; ++i) {TT= Hash (X+xx[i], y+yy[i], z+Zz[i]); if(Ufs[tt] = =-2)Continue; if(FindRoot (to) = = FindRoot (TT)) flag =true; Mergeufs (t, TT); } returnFlag;}voidinput () {memset (UFS,-1,sizeof(UFS)); intT; scanf ("%d", &N); for(inti =0; I < n; ++i) {scanf ("%d%d%d", &x[i], &y[i], &Z[i]); T=Hash (X[i], y[i], z[i]); Ufs[t]= -2; } for(inti =0; i < MaxX; ++i) for(intj =0; J < MaxX; ++j) {T= Hash (i, J,0); Ufs[t]= -2; } for(intK =1; K < MaxX; ++k) for(inti =0; i < MaxX; ++i) for(intj =0; J < MaxX; ++j) Dfs (I, j, K);}voidWork () { for(inti = n1; I >=0; i--) { if(!judge (X[i], y[i], z[i])) {printf ("no\n"); return; }} printf ("yes\n");}intMain () {//freopen ("test.in", "R", stdin); //freopen ("Test.out", "w", stdout); intT; scanf ("%d", &T); for(intTimes =1; Times <= T; ++Times ) {input (); Work (); } return 0;}
View Code
ACM Learning process-hihocoder 1291 Building in Sandbox (Dfs && offline && and collection)