ZOJ 월 경기 November 2010__Cube Simulation

Cube Simulation
Time Limit: 2 Seconds      Memory Limit: 65536 KB
Here's a cube whose size of its 3 dimensions are all infinite. Meanwhile, there're 6 programs operating this cube:
  • FILL(X,Y,Z): Fill some part of the cube with different values.
    	memset(cube, 0, sizeof(cube));
    	puts("START");
    	cnt = 0;
    	for (int i = 0; i < X; i++) {
    		for (int j = 0; j < Y; j++) {
    			for (int k = 0; k < Z; k++) {
    				cube[i][j][k] = ++cnt;
    			}
    		}
    	}
    
  • SWAP1(x1,x2): Swap two sub-cube along the first dimensions.
    	for (int j = 0; j < Y; j++) {
    		for (int k = 0; k < Z; k++) {
    			exchange(cube[x1][j][k], cube[x2][j][k]);
    		}
    	}
    
  • SWAP2(y1,y2): Swap two sub-cube along the second dimensions.
    	for (int i = 0; i < X; i++) {
    		for (int k = 0; k < Z; k++) {
    			exchange(cube[i][y1][k], cube[i][y2][k]);
    		}
    	}
    
  • SWAP3(z1,z2): Swap two sub-cube along the third dimensions.
    	for (int i = 0; i < X; i++) {
    		for (int j = 0; j < Y; j++) {
    			exchange(cube[i][j][z1], cube[i][j][z2]);
    		}
    	}
    
  • FIND(value): Output the value's location, if it exist.
    	for (int i = 0; i < X; i++) {
    		for (int j = 0; j < Y; j++) {
    			for (int k = 0; k < Z; k++) {
    				if (cube[i][j][k] == value) {
    					printf("%d %d %d/n", i, j, k);
    				}
    			}
    		}
    	}
    
  • QUERY(x,y,z): Output the value at (x,y,z).
    	printf("%d/n", cube[x][y][z]);
    

  • We'll give a list of operations mentioned above. Your job is to simulate the program and tell us what does the machine output in progress.

    Input


    There'll be 6 kind of operations in the input.
  • FILL X Y Z (1 <= X, Y, Z <= 1000) for FILL(X,Y,Z)
  • SWAP1 X1 X2 (0 <= X1, X2 < X) for SWAP1(X1,X2)
  • SWAP2 Y1 Y2 (0 <= Y1, Y2 < Y) for SWAP2(Y1,Y2)
  • SWAP3 Z1 Z2 (0 <= Z1, Z2 < Z) for SWAP3(Z1,Z2)
  • FIND value (value > 0) for FIND(value)
  • QUERY x y z (0 <= x < X, 0 <= y < Y, 0 <= z < Z) for QUERY(x,y,z)

  • The input will always start with FILL operation and terminate by EOF.
    The number of the operations will less than 200,000, while the FILL operation will less than 100.

    Output


    Simulate all of the operations in order, and print the output of the programs.

    Sample Input

    FILL 2 3 1
    SWAP1 0 1
    SWAP2 0 2
    SWAP3 0 0
    FIND 1
    FIND 2
    FIND 3
    FIND 4
    FIND 5
    FIND 6
    FIND 7
    QUERY 0 0 0
    QUERY 0 1 0
    QUERY 0 2 0
    QUERY 1 0 0
    QUERY 1 1 0
    QUERY 1 2 0
    

    Sample Output

    START
    1 2 0
    1 1 0
    1 0 0
    0 2 0
    0 1 0
    0 0 0
    6
    5
    4
    3
    2
    1
    

    Hint


    exchange(x,y) means exchange the value of variable x and y.
    Because of HUGE input and output, scanf and printf is recommended.
     
     
     
     
    사고방식: 최초의 좌표와 값의 대응 상황에 따라 몇 차례의 좌표 변환을 하고 최종적으로 원시 좌표에 따라 해당하는 값을 찾는다(관건은 좌표 변환의 등식을 확정한다)
    AC 코드:
    #include<stdio.h>
    #include<string.h>
    int x0[1010];
    int y0[1010];
    int z0[1010];
    int tx,ty,tz;
    void fill()
    {
    	int i;
    	for(i=0;i<tx;i++)
    		x0[i]=i;
    	for(i=0;i<ty;i++)
    		y0[i]=i;
    	for(i=0;i<tz;i++)
    		z0[i]=i;
    	puts("START");
    
    }
    void swap1(int x1,int x2)
    {
    	int t;
    	t=x0[x1];
    	x0[x1]=x0[x2];
    	x0[x2]=t;
    }
    void swap2(int y1,int y2)
    {
    	int t;
    	t=y0[y1];
    	y0[y1]=y0[y2];
    	y0[y2]=t;
    }
    void swap3(int z1,int z2)
    {
    	int t;
    	t=z0[z1];
    	z0[z1]=z0[z2];
    	z0[z2]=t;
    }
    void query(int x,int y,int z)
    {
    	
    	x=x0[x];
    	y=y0[y];
    	z=z0[z];
    	int s;
    	s=z+y*tz+x*ty*tz+1;// 
    	printf("%d/n",s);
    }
    void find(int v)
    {
    	if(v>tx*ty*tz)
    		return;
    	int x,y,z;
    	x=(v-1)/(ty*tz);// 
    	y=((v-1)%(ty*tz))/tz;
    	z=((v-1)%(ty*tz))%tz;
    	int i;
    	for(i=0;i<tx;i++)
    	{
    		if(x0[i]==x)
    		{
    			x=i;
    			break;
    		}
    	}
    	for(i=0;i<ty;i++)
    	{
    		if(y0[i]==y)
    		{
    			y=i;
    			break;
    		}
    	}
    	for(i=0;i<tz;i++)
    	{
    		if(z0[i]==z)
    		{
    			z=i;
    			break;
    		}
    	}
    	printf("%d %d %d/n",x,y,z);
    }
    
    int main()
    {
    	char op[20];
    	while(scanf("%s",op)!=EOF)
    	{
    		int a,b,c;
    		if(strcmp(op,"FILL")==0)
    		{
    			scanf("%d%d%d",&tx,&ty,&tz);
    			fill();
    		}
    		else if(strcmp(op,"SWAP1")==0)
    		{
    			
    			scanf("%d%d",&a,&b);
    			swap1(a,b);
    		}
    		else if(strcmp(op,"SWAP2")==0)
    		{
    		
    			scanf("%d%d",&a,&b);
    			swap2(a,b);
    		}
    		else if(strcmp(op,"SWAP3")==0)
    		{
    		
    			scanf("%d%d",&a,&b);
    			swap3(a,b);
    		}
    		else if(strcmp("FIND",op)==0)
    		{
    			int v;
    			scanf("%d",&v);
    			find(v);
    		}
    		else if(strcmp("QUERY",op)==0)
    		{
    			scanf("%d%d%d",&a,&b,&c);
    			query(a,b,c);
    		}
    	}
    	return 0;
    }
    
    
    

    좋은 웹페이지 즐겨찾기