Buiding in Sandbox--2016微軟預科生技術崗筆試題四--Java

問題:
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

package hihocoder;

import java.util.Scanner;

public class Main {
    boolean mark=true;
    int count=0;
    public static void main(String args[]){
        Main m=new Main();
        m.getInput();
    }

    public void getInput(){
        Scanner in=new Scanner(System.in);
        int t=in.nextInt();
        for(int i=0;i<t;i++){
            count=0;
            mark=true;
            int num=in.nextInt();
            int[]X=new int[num];
            int[]Y=new int[num];
            int[]Z=new int[num];
            for(int j=0;j<num;j++){
                int x=in.nextInt();
                int y=in.nextInt();
                int z=in.nextInt();
                setArray(x,y,z,X,Y,Z);
            }
            if(mark){
                System.out.println("Yes");
            }else
                System.out.println("No");
        }
        in.close();
    }
    public void setArray(int i,int j,int k,int[]X,int[]Y,int[]Z){
        boolean mark1=false;
        X[count]=i;
        Y[count]=j;
        Z[count]=k;
        count++;
        if(k>1){
            for(int q=0;q<count-1;q++){
                int i1=Math.abs(i-X[q]);
                int i2=Math.abs(j-Y[q]);
                int i3=Math.abs(k-Z[q]);
                if(i1+i2+i3==1){
                    mark1=true;
                    break;
                }
            }
            mark=mark1&&mark;
        }
    }
}


以上程序自己在eclipse中測試,都沒有問題,但是提交都顯示WA,爲什麼呢?自己的測試集不夠嗎?歡迎各路大神指教!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章