zoj 4113 Cube Simulation ( ZOJ Monthly, November 2010 )

题目地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4113

思路:这个就是三维数组的地址和下标之间的对应。  然后涉及到一定的变换 ,我们有一个p【a】 记录在若干次对换之后实际对应的是哪个数。 同时,为了能用find函数找回坐标,我们还要记录反函数(写作pp【a】)

然后这个value超过的xyz是不要输出的。

代码:

#include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
using namespace std;



int p[1010];
int q[1010];
int r[1010];

int pp[1010];
int qq[1010];
int rr[1010];

int x=1,y=1,z=1;

int findx(int n)
{
   return (n)/(y*z);

}

int findy(int n)
{
   return (n%(y*z))/z;

}

int findz(int n)
{
   return (n%z);
}

void  pre()
{
   for(int i=0;i<1010;i++)
    {
      p[i]=i;
      q[i]=i;
      r[i]=i;
      pp[i]=i;
      qq[i]=i;
      rr[i]=i;
    }

}


int main()
{
     string opcode;
     int a,b;
     int ansx,ansy,ansz;
     int value;

     int qx,qy,qz;

     int ans;

     while(cin>>opcode)
     {
         if(opcode=="FILL")
         {
              pre();
              cout<<"START"<<endl;
              cin>>x>>y>>z;


         }

         else if(opcode=="SWAP1")
         {
             scanf("%d%d",&a,&b);


             swap(p[a],p[b]);
             pp[p[a]]=a;
             pp[p[b]]=b;

         }


          else if(opcode=="SWAP2")
         {

             scanf("%d%d",&a,&b);


             swap(q[a],q[b]);
              qq[q[a]]=a;
              qq[q[b]]=b;

         }

          else if(opcode=="SWAP3")
         {
            scanf("%d%d",&a,&b);

              swap(r[a],r[b]);

              rr[r[a]]=a;
              rr[r[b]]=b;
         }


         else if(opcode=="FIND")
         {
             scanf("%d",&value);

             if(value<=x*y*z&&value>0)
             {

             ansx=findx(value-1);
             ansy=findy(value-1);
             ansz=findz(value-1);

             ansx=pp[ansx];
             ansy=qq[ansy];
             ansz=rr[ansz];

             printf("%d %d %d\n",ansx,ansy,ansz);

             }
         }

         else if(opcode=="QUERY")
         {
              scanf("%d%d%d",&qx,&qy,&qz);

              if(qx>=0&&qx<x&&qy>=0&&qy<y&&qz>=0&&qz<z)
              {

              ans=p[qx]*y*z+q[qy]*z+r[qz];

              printf("%d\n",ans+1);

              }
         }
     }
     }


posted on 2013-09-20 21:58  814jingqi的ACM  阅读(112)  评论(0编辑  收藏  举报