ZOJ Problem Set - 3429 Cube Simulation
题目描述:
题目链接:ZOJ Problem Set - 3429 Cube Simulation
题目大意:
根据题目描述,模拟在空间中的6种操作。对应不同输入执行对应的操作。
解题思路:
题目的关键点在于,value的值和X,Y,Z坐标的转化关系。将题目描述的一个三维数组,拆分为 x[1000],y[1000],z[1000] 三个轴。同时,可通过枚举数据的方式推出 value=x[a]∗Y∗Z+y[b]∗Z+z[c]+1 将题目所要求的操作,用该公式来表示即可。
复杂度分析:
时间复杂度:
O(n)
空间复杂度:
O(n)
AC代码:
#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <cctype>
#include <algorithm>
#include <cstdlib>
using namespace std;
const int maxn = 1010;
int x[maxn],y[maxn],z[maxn];
int X,Y,Z;
int value,a,b,c;
void FILL()
{
scanf("%d%d%d",&X,&Y,&Z);
puts("START");
for(int i = 0; i < X; i++) x[i] = i;
for(int i = 0; i < Y; i++) y[i] = i;
for(int i = 0; i < Z; i++) z[i] = i;
}
void SWAP1()
{
int x1,x2;
scanf("%d%d",&x1,&x2);
swap(x[x1], x[x2]);
}
void SWAP2()
{
int y1,y2;
scanf("%d%d",&y1,&y2);
swap(y[y1], y[y2]);
}
void SWAP3()
{
int z1,z2;
scanf("%d%d",&z1,&z2);
swap(z[z1], z[z2]);
}
void FIND()
{
scanf("%d" ,&value);
if(value > X*Y*Z)return;
value--;
c = value % Z;
value /= Z;
b = value % Y;
value /= Y;
a = value;
for(int i = 0; i < X; i++)
if(x[i] == a)
{
a = i;
break;
}
for(int i = 0; i < Y; i++)
if(y[i] == b)
{
b = i;
break;
}
for(int i = 0; i < Z; i++)
if(z[i] == c)
{
c = i;
break;
}
printf("%d %d %d\n",a,b,c);
}
void QUERY()
{
scanf("%d%d%d",&a,&b,&c);
value = x[a]*Y*Z+y[b]*Z+z[c]+1;
printf("%d\n",value);
}
int main()
{
char op[6];
while(scanf("%s",op) != EOF)
{
if(strcmp(op,"FILL") == 0) FILL();
else if(strcmp(op,"SWAP1") == 0) SWAP1();
else if(strcmp(op,"SWAP2") == 0) SWAP2();
else if(strcmp(op,"SWAP3") == 0) SWAP3();
else if(strcmp(op,"FIND") == 0) FIND();
else if(strcmp(op,"QUERY") == 0) QUERY();
}
return 0;
}