zoukankan      html  css  js  c++  java
  • Cube Simulation zoj3429 模拟

    Description
    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
    ", i, j, k);
                }
            }
        }
    }
    

    QUERY(x,y,z): Output the value at (x,y,z).

    printf("%d
    ", 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.

    (1)题意:给一些操作函数,问一定数量的操作给出,会输出什么。
    解法:由于code3维数组。实际作用是计数器,所以给出x,y,z的值能够直接得出3维数组所代表的数组的值。即Value=tx[a]*Y*Z+ty[b]*Z+tz[c]+1;而其它操作,交换也是相当于是交换坐标,给出value,也能够模拟得出x,y,z的值。

    tx,ty,tz数组分别模拟x,y,z的坐标值。

    #include<cstdio>
    #include<cmath>
    #include<cstring>
    #include<algorithm>
    #include<iostream>
    using namespace std;
    int X,Y,Z;
    int tx[1005],ty[1005],tz[1005];
    void FILL()
    {
        puts("START");
        int i,j,k;
        for (i = 0; i < X; i++) tx[i]=i;
        for (j = 0; j < Y; j++) ty[j]=j;
        for (k = 0; k < Z; k++) tz[k]=k;
    }
    void SWAP1(int x1,int x2)
    {
        swap(tx[x1],tx[x2]);
    }
    void SWAP2(int y1,int y2)
    {
        swap(ty[y1],ty[y2]);
    }
    void SWAP3(int z1,int z2)
    {
        swap(tz[z1],tz[z2]);
    }
    void FIND(int value)
    {
        value--;            
        int ttz=value%Z;
        value=value/Z;      
        int tty=value%Y;
        value=value/Y;     
        int ttx=value;
    
        int i,j,k,p;
        for(p=0;p<X;p++)
        {
            if(tx[p]==ttx)
            {
                i=p;
                break;
            }
        }
        for(p=0;p<Y;p++)
        {
            if(ty[p]==tty)
            {
                j=p;
                break;
            }
        }
        for(p=0;p<Z;p++)
        {
            if(tz[p]==ttz)
            {
                k=p;
                break;
            }
        }
        if(i<X&&j<Y&&k<Z) printf("%d %d %d
    ",i,j,k);
    }
    void QUERY(int a,int b,int c)
    {
        int fuck=tx[a]*Y*Z+ty[b]*Z+tz[c]+1;
        printf("%d
    ",fuck);
    }
    int main()
    {
        char a[20];
        while(scanf("%s",a)!=EOF)
        {
            int i;
            if(strcmp(a,"FILL")==0)
            {
                scanf("%d %d %d",&X,&Y,&Z);
                FILL();
            }
            else if(strcmp(a,"SWAP1")==0)
            {
                int x1,x2;
                scanf("%d %d",&x1,&x2);
                SWAP1(x1,x2);
            }
           else if(strcmp(a,"SWAP2")==0)
            {
                int y1,y2;
                scanf("%d %d",&y1,&y2);
                SWAP2(y1,y2);
            }
            else if(strcmp(a,"SWAP3")==0)
            {
                int z1,z2;
                scanf("%d %d",&z1,&z2);
                SWAP3(z1,z2);
            }
            else if(strcmp(a,"FIND")==0)
            {
                int value;
                scanf("%d",&value);
                FIND(value);
            }
            else if(strcmp(a,"QUERY")==0)
            {
                int x,y,z;
                scanf("%d %d %d",&x,&y,&z);
                QUERY(x,y,z);
            }
        }
        return 0;
    }
  • 相关阅读:
    使用git bash提交代码到github托管
    电子邮件的正则表达式
    PHP正则表达式及实例
    php中session_start()函数的作用
    mysql 中文乱码
    mysql 安装以及配置
    高质量JAVA代码编写规范
    DAO设计模式
    深入浅出UML类图
    分析业务模型-类图(Class Diagram)
  • 原文地址:https://www.cnblogs.com/llguanli/p/8426423.html
Copyright © 2011-2022 走看看