ZOJ 3429 Cube Simulation

链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3429


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.


Author: OUYANG, Jialin
Contest: ZOJ Monthly, November 2010


大意——给你一个三维数组,你可以对它进行六种操作。现在给你一系列操作,你的任务就是去模拟它,然后给出在运行过程中,会输出什么东西。


思路——因为数组不能开得太大,所以不能直接去模拟。注意到填充数组时的规律,cube[i][j][k]=k+j*Z+i*Y*Z+1,其中X,Y,Z为填充数组时给出的范围大小。再稍微注意一下,可以发现,交换两个值时,只需要交换其相应的索引的值即可。那么最后给你索引,你就只需要找到索引的值,然后按照上述公式计算即可。如果给你值,要你找索引,那你就只需要逆用上述公式找到索引的值,然后再找索引即可解决。


复杂度分析——时间复杂度:O(X+Y+Z),空间复杂度:O(X+Y+Z)


附上AC代码:


#include <iostream>
#include <cstdio>
#include <string>
#include <cmath>
#include <iomanip>
#include <ctime>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <set>
#include <map>
using namespace std;
typedef unsigned int UI;
typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;
const double pi = acos(-1.0);
const double e = exp(1.0);
const double eps = 1e-8;
const int maxx = 1005;
int cube_x[maxx];
int cube_y[maxx];
int cube_z[maxx]; // 用来存储数组索引
int X, Y, Z; // 数组大小
char str[10]; // 键入命令

void fill(int x, int y, int z); // 填充数组索引
void swap1(int x, int y); // 交换第一个索引
void swap2(int x, int y); // 交换第二个索引
void swap3(int x, int y); // 交换第三个索引
void find(int value); // 找到此值的索引
void query(int x, int y, int z); // 找到此索引的值

int main()
{
	ios::sync_with_stdio(false);
	int x, y, z, value;
	while (~scanf("%s", str))
	{
		if (strcmp("FILL", str) == 0)
		{
			scanf("%d%d%d", &X, &Y, &Z);
			fill(X, Y, Z);
		}
		else if (strcmp("SWAP1", str) == 0)
		{
			scanf("%d%d", &x, &y);
			swap1(x, y);
		}
		else if (strcmp("SWAP2", str) == 0)
		{
			scanf("%d%d", &x, &y);
			swap2(x, y);
		}
		else if (strcmp("SWAP3", str) == 0)
		{
			scanf("%d%d", &x, &y);
			swap3(x, y);
		}
		else if (strcmp("FIND", str) == 0)
		{
			scanf("%d", &value);
			find(value);
		}
		else
		{
			scanf("%d%d%d", &x, &y, &z);
			query(x, y, z);
		}
	}
	return 0;
}

void fill(int x, int y, int z)
{
	printf("START\n");
	for (int i=0; i<x; ++i)
		cube_x[i] = i;
	for (int j=0; j<y; ++j)
		cube_y[j] = j;
	for (int k=0; k<z; ++k)
		cube_z[k] = k;
}

void swap1(int x, int y)
{
	swap(cube_x[x], cube_x[y]);
}

void swap2(int x, int y)
{
	swap(cube_y[x], cube_y[y]);
}

void swap3(int x, int y)
{
	swap(cube_z[x], cube_z[y]);
}

void find(int value)
{
	if (value > X*Y*Z)
		return ;
	int x, y, z;
	x = (value-1)/(Y*Z); // 计算第一个索引
	y = ((value-1)%(Y*Z))/Z; // 计算第二个索引
	z = ((value-1)%(Y*Z))%Z; // 计算第三个索引
	for (int i=0; i<X; ++i)
		if (cube_x[i] == x)
		{
			x = i; // 因为没交换之前索引与数组元素值相等,
				   // 交换之后不一定相等,所以找回原来的
			break;
		}
	for (int j=0; j<Y; ++j)
		if (cube_y[j] == y)
		{
			y = j;
			break;
		}
	for (int k=0; k<Z; ++k)
		if (cube_z[k] == z)
		{
			z = k;
			break;
		}
	printf("%d %d %d\n", x, y, z);
}

void query(int x, int y, int z)
{
	x = cube_x[x];
	y = cube_y[y];
	z = cube_z[z]; // 找到改变后的索引
	int ans = z+y*Z+x*Y*Z+1; // 因为索引从0开始,所以加上1
	printf("%d\n", ans);
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值