比赛描述
A polycube is a solid made by gluing together unit cubes (one unit on each edge) on one or morefaces. The figure in the lower-left is not a polycube because some cubes are attached along anedge.
For this problem, the polycube will be formed from unit cubes centered at integer lattice points in3-space. The polycube will be built up one cube at a time, starting with a cube centered at (0,0,0). Ateach step of the process (after the first cube), the next cube must have a face in common with a cubepreviously included and not be the same as a block previously included. For example, a 1 -by-1 -by-5block (as shown above in the upper-left polycube) could be built up as:
(0,0,0) (0,0,1 ) (0,0,2) (0,0,3) (0,0,4)
and a 2-by-2-by-2 cube (upper-right figure) could be built as:
(0,0,0) (0,0,1 ) (0,1,1 ) (0,1, 0) (1,0,0) (1,0,1 ) (1,1,1 ) (1,1, 0)
Since the surface of the polycube is made up of unit squares, its area is an integer.Write a program which takes as input a sequence of integer lattice points in 3-space and determineswhether is correctly forms a polycube and, if so, what the surface area of the polycube is.
输入
The first line of input contains a single integer N, (1 £ N £ 1000) which is the number of data sets thatfollow. Each data set consists of multiple lines of input. The first line contains the number of points P,(1 = P = 100) in the problem instance. Each succeeding line contains the centers of the cubes, eightto a line (except possibly for the last line). Each center is given as 3 integers, separated by commas.The points are separated by a single space.
输出
For each data set, you should generate one line of output with the following values: The data setnumber as a decimal integer (start counting at one), a space and the surface area of thepolycube if itis correctly formed, OR, if it is not correctly formed, the string “NO” a space and the index (startingwith 1) of the first cube which does not share a face with a previous cube. Note that the surface areaincludes the area of any included holes.
样例输入
4
5
0,0,0 0,0,1 0,0,2 0,0, 3 0,0,4
8
0,0,0 0,0,1 0,1,0 0,1,1 1,0,0 1,0,1 1,1,0 1,1,1
4
0,0,0 0,0,1 1,1,0 1,1,1
20
0,0,0 0,0,1 0,0,2 0,1,2 0,2,2 0,2,1 0,2,0 0,1,0
1,0,0 2,0,0 1,0,2 2,0,2 1,2,2 2,2,2 1,2,0 2,2,0
2,1,0 2,1,2 2,0,1 2,2,1
样例输出
1 22
2 24
3 NO 3
4 72
提示
undefined
题目来源
ACM ICPC Greater New York Region 2008
#include<stdio.h> #include<memory> #define N 101 bool exist[N][N][N]; int neighborX[6] = { 1,-1, 0, 0, 0, 0}; //有公共面的六个邻居 int neighborY[6] = { 0, 0, 1,-1, 0, 0}; int neighborZ[6] = { 0, 0, 0, 0, 1,-1}; int main(){ // freopen("1537.txt","r",stdin); int t,T,n,i,j,x,y,z,surface,connected,firstNotConnect; bool isOK; scanf("%d",&T); for(t=1; t<=T; t++){ scanf("%d",&n); memset(exist,0,sizeof(exist)); scanf("%d,%d,%d",&x,&y,&z); exist[x][y][z] = 1; surface = 6; isOK = 1; //标志到现在为止所有方块都成功地连在一起 for(i=2; i<=n; i++){ scanf("%d,%d,%d",&x,&y,&z); if(!isOK){ continue; } if(exist[x][y][z]){ isOK = 0; firstNotConnect = i; continue; } connected = 0; for(j=0; j<6; j++){ if(x+neighborX[j]>=0 && y+neighborY[j]>=0 && z+neighborZ[j]>=0 && exist[x+neighborX[j]][y+neighborY[j]][z+neighborZ[j]]){ connected++; } } if(!connected){ isOK = 0; firstNotConnect = i; continue; } surface += (6-connected*2); exist[x][y][z] = 1; } if(!isOK){ printf("%d NO %d\n",t,firstNotConnect); }else{ printf("%d %d\n",t,surface); } } }