ZOJ Problem Set - 3429 Cube Simulation

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]YZ+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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章