Building in Sandbox

Source: Internet
Author: User

The subject comes from hihocoder#1291 time limit: 30000ms single point limit: 3000ms memory limit: 256MB description

Little Hi is playing a sandbox voxel game. In the game, the whole world was constructed by massive 1x1x1 cubes. The edges of cubes is parallel to the coordinate axes and the coordinates (x, y, z) of the center of each cube is intege Rs.

At the beginning there are nothing but plane ground in the world. The ground consists of all the cubes of z=0. Little Hi needs to build everything by placing cubes one by one following the rules:

1. The newly placed cube must is adjacent to the ground or a previously placed cube. The cubes is adjacent if and only if they share a same the face.

2. The newly placed cube must is accessible from outside which means by moving in 6 directions (up, down, left, right, forw ARD, backward) there is a path from a very far place-say ((+, +, +) in this problem-to this cube without Passi Ng through ground or other cubes.

Given a sequence of cubes Little Hi wants to know if he can build the world by placing the cubes in such order.

Input

The first line contains the number of test cases T (1 <= T <= 10).

For each test case, the first line was N the number of cubes in the sequence.

The following N lines each contain three integers x, y and z indicating the coordinates of a cube.

For 20% of the data, 1 <= N <=, 1 <= x, y, z <= 10.

For 100% of the data, 1 <= N <= 100000, 1 <= x, y, z <= 100.

Output

For each testcase output ' Yes ' or ' No ' indicating if Little Hi can place the ' cubes in such order.

Sample Hints

The first test case three cubes is placed on the ground. It ' s OK.

In the second test case (1, 3, 2) are neither on the ground nor adjacent to previous cubes. So it can ' t be placed.

The last Test case (2, 2, 1) can is reached from outside. So it can ' t be placed.

Sample input
331 1 11 2 11 3 131 1 11 2 11 3 2171 1 11 2 11 3 12 3 13 3 13 2 13 1 12 1 12 1 21 1 21 2 21 3 22 3 23 3 23 2 22 2 22 2 1
Sample output
Yesnono

For this problem, we need to detect whether a block sequence can be placed, just check two conditions, the first condition is to check if the new block is placed adjacent to the block, this is easy to do. The key is that the second condition, we need to check whether the new block can lead to infinity. A basic idea is to use FloodFill, but if you use this method directly for each block, our algorithm will time out. One idea is that we want to be able to dynamically maintain which places can lead to infinity, but the problem is that it's hard to tell if a block's operation will cause an area to be closed. So the point of this problem is here, if we put the box in order, it is difficult to determine whether the block will be enclosed in a few areas, but if we consider a reverse process, to take the square out? Each time we take a block, we take a floodfill at the place where we took it, so we can figure out which external points we added after we removed the block. At the same time, we just need to see if a block is adjacent to an external point, and we can tell if we can get rid of the block. It is easy to verify that we can place the block sequentially, when and only if the adjacent conditions are met, and we can remove the block in reverse.

The code is as follows:

#include <iostream>#include<vector>#include<deque>#include<stack>#include<string>#include<algorithm>#include<string.h>#include<sstream>structPoint {intx, y, Z; Point (): X (0), Y (0), Z (0) {}; Point (intAintBintc): X (a), Y (b), Z (c) {}};intMax_c;Const intMax_n =100000+5;Const intdir[6][3] = {    {1,0,0},    {0,1,0},    {0,0,1},    {-1,0,0},    {0, -1,0},    {0,0, -1}};intpos[ -+2][ -+2][ -+2];intis_out[ -+2][ -+2][ -+2]; Point Cors[max_n];using namespacestd;BOOLHas_adj (intXintYintz) {if(Z = =1)return true;  for(intD =0; D <6; ++d) {intX1 = x + dir[d][0]; intY1 = y + dir[d][1]; intZ1 = z + dir[d][2]; if(POS[X1][Y1][Z1])return true; }    return false;}BOOLCan_take (intXintYintz) { for(intD =0; D <6; ++d) {intX1 = x + dir[d][0]; intY1 = y + dir[d][1]; intZ1 = z + dir[d][2]; if(IS_OUT[X1][Y1][Z1])return true; }    return false;}voidMY_BFS (intXintYintz) {deque<Point>Q;    Point P0 (x, y, z);    Q.push_back (P0);  while(!Q.empty ()) {Point P= q[0];        Q.pop_front (); if(P.z <1|| P.z > Max_c +1|| P.x <0|| p.x > Max_c +1|| P.y <0|| P.y > Max_c +1)Continue; if(Is_out[p.x][p.y][p.z] | | pos[p.x][p.y][p.z])Continue; IS_OUT[P.X][P.Y][P.Z]=1;  for(intD =0; D <6; ++d) {intX1 = p.x + dir[d][0]; intY1 = p.y + dir[d][1]; intZ1 = p.z + dir[d][2];            Point P1 (x1, y1, Z1);        Q.push_back (p1); }        }}intMain () {intT; CIN>>T;  for(intt =0; T < T; ++t) {intN; CIN>>N; memset (POS,0,sizeof(POS)); memset (Is_out,0,sizeof(is_out)); BOOLIs_possible =true; Max_c=0;  for(inti =0; i < N; ++i) {intx, y, Z; CIN>> x >> y >>Z; cors[i].x=x; Cors[i].y=y; Cors[i].z=Z; Max_c=Max (x, Max_c); Max_c=Max (y, Max_c); Max_c=Max (z, Max_c); if(!pos[x][y][z] &&Has_adj (x, Y, z)) {Pos[x][y][z]=1; }            Else{is_possible=false; //Break ;            }        }        if(!is_possible) {cout<<"No"<<Endl; Continue; }        Else{MY_BFS (0,0,1);  for(inti = N-1; I >=0; --i) {intx =cors[i].x; inty =cors[i].y; intz =cors[i].z; if(!can_take (x, Y, z)) {is_possible=false;  Break; } Pos[x][y][z]=0;            My_bfs (x, y, z); }            if(is_possible) cout <<"Yes"<<Endl; Elsecout <<"No"<<Endl; }    }    //System ("pause");    return 0;}

Building in Sandbox

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.