描述
Little Hi is playing a sandbox voxel game. In the game the whole world is constructed by massive 1x1x1 cubes. The edges of cubes are parallel to the coordinate axes and the coordinates (x, y, z) of the center of each cube are integers.
At the beginning there is 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 be adjacent to the ground or a previously placed cube. Two cubes are adjacent if and only if they share a same face.
2. The newly placed cube must be accessible from outside which means by moving in 6 directions(up, down, left, right, forward, backward) there is a path from a very far place - say (1000, 1000, 1000) in this problem - to this cube without passing 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.
输入
The first line contains the number of test cases T(1 <= T <= 10).
For each test case the first line is 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 <= 1000, 1 <= x, y, z <= 10.
For 100% of the data, 1 <= N <= 100000, 1 <= x, y, z <= 100.
输出
For each testcase output "Yes" or "No" indicating if Little Hi can place the cubes in such order.
样例提示
In the first test case three cubes are placed on the ground. It's OK.
In the second test case (1, 3, 2) is neither on the ground nor adjacent to previous cubes. So it can't be placed.
In the last test case (2, 2, 1) can not be reached from outside. So it can't be placed.
3 3 1 1 1 1 2 1 1 3 1 3 1 1 1 1 2 1 1 3 2 17 1 1 1 1 2 1 1 3 1 2 3 1 3 3 1 3 2 1 3 1 1 2 1 1 2 1 2 1 1 2 1 2 2 1 3 2 2 3 2 3 3 2 3 2 2 2 2 2 2 2 1
Yes No No
思路:用并查集解决!但是没有AC只有60分,所以思路可能是错的,就不说思路了,贴下代码,希望有路过的大神,帮忙找找bug
代码如下,笔试时未AC
#include<set>
#include<map>
#include<list>
#include<queue>
#include<stack>
#include<string>
#include<time.h>
#include<math.h>
#include<memory>
#include<vector>
#include<bitset>
#include<fstream>
#include<stdio.h>
#include<utility>
#include<sstream>
#include<string.h>
#include<iostream>
#include<stdlib.h>
#include<algorithm>
using namespace std;
bool a[109][109][109];
int father[1500005];
int xx[100005],yy[100005],zz[100005];
int findroot(int x)
{
int root;
for (root=x;father[root]!=-1;root=father[root]) ;
int temp;
for (;father[x]!=-1;)
{
temp=father[x];
father[x]=root;
x=temp;
}
return root;
}
void u(int x,int y)
{
x=findroot(x);
y=findroot(y);
if (x==y) return;
father[x]=y;
}
int id(int x,int y,int z)
{
return x*107*107+y*107+z;
}
int main()
{
int t;
scanf("%d",&t);
int zu;
for (zu=0;zu<t;zu++)
{
memset(a,0,sizeof(a));
memset(father,-1,sizeof(father));
int n;
scanf("%d",&n);
bool failed=false;
int i;
for (i=0;i<n;i++)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
xx[i]=x;
yy[i]=y;
zz[i]=z;
a[x][y][z]=true;
if ((a[x-1][y][z])||(a[x+1][y][z])||(a[x][y-1][z])||(a[x][y+1][z])||(a[x][y][z-1])||(a[x][y][z+1])||(z==1))
{
}
else
{
failed=true;
}
}
for (i=1;i<=103;i++)
{
int j,k;
for (j=1;j<=103;j++)
{
for (k=1;k<=103;k++)
{
if (a[i][j][k]) continue;
if (!a[i+1][j][k]) u(id(i,j,k),id(i+1,j,k));
if (!a[i-1][j][k]) u(id(i,j,k),id(i-1,j,k));
if (!a[i][j+1][k]) u(id(i,j,k),id(i,j+1,k));
if (!a[i][j-1][k]) u(id(i,j,k),id(i,j-1,k));
if (!a[i][j][k+1]) u(id(i,j,k),id(i,j,k+1));
if (!a[i][j][k-1]) u(id(i,j,k),id(i,j,k-1));
}
}
}
int t;
for (t=n-1;t>=0;t--)
{
int i=xx[t];
int j=yy[t];
int k=zz[t];
a[i][j][k]=false;
if (!a[i+1][j][k]) u(id(i,j,k),id(i+1,j,k));
if (!a[i-1][j][k]) u(id(i,j,k),id(i-1,j,k));
if (!a[i][j+1][k]) u(id(i,j,k),id(i,j+1,k));
if (!a[i][j-1][k]) u(id(i,j,k),id(i,j-1,k));
if (!a[i][j][k+1]) u(id(i,j,k),id(i,j,k+1));
if (!a[i][j][k-1]) u(id(i,j,k),id(i,j,k-1));
if (findroot(id(i,j,k))!=findroot(id(101,101,101))) failed=true;
}
if (failed)
{
puts("No");
}
else
{
puts("Yes");
}
}
return 0;
}