模拟,标记六个方向的位置为0,1,2。。。,然后记录当前位置六种走法所要朝向的方向,而我们所需要的就是每个位置的这几个方向,很显然,其中的front所得到的方向就是当前所朝向的方向。
#include <iostream> #include <cstdio> #include <cstring> using namespace std; struct node { int front,back,left,right,up,down; }; node b_ch(node op) { node tem; tem.front=op.back; tem.back=op.front; tem.left=op.right; tem.right=op.left; tem.up=op.up; tem.down=op.down; return tem; } node l_ch(node op) { node tem; tem.front=op.left; tem.back=op.right; tem.left=op.back; tem.right=op.front; tem.up=op.up; tem.down=op.down; return tem; } node r_ch(node op) { node tem; tem.front=op.right; tem.back=op.left; tem.left=op.front; tem.right=op.back; tem.up=op.up; tem.down=op.down; return tem; } node u_ch(node op) { node tem; tem.front=op.up; tem.back=op.down; tem.left=op.left; tem.right=op.right; tem.up=op.back; tem.down=op.front; return tem; } node d_ch(node op) { node tem; tem.front=op.down; tem.back=op.up; tem.left=op.left; tem.right=op.right; tem.up=op.front; tem.down=op.back; return tem; } int main() { int m; cin>>m; while(m--) { int n; scanf("%d",&n); char op[30]; int i,dis; node td; td.front=0,td.back=3,td.left=4,td.right=1,td.up=2,td.down=5; int x=0,y=0,z=0; for(i=0;i<n;i++) { scanf("%s %d",op,&dis); if(strcmp(op,"forward")==0) { td=td; } else if(strcmp(op,"back")==0) {td=b_ch(td);} else if(strcmp(op,"left")==0) {td=l_ch(td);} else if(strcmp(op,"right")==0) {td=r_ch(td);} else if(strcmp(op,"up")==0) {td=u_ch(td);} else {td=d_ch(td);} if(td.front==0) {x+=dis;} else if(td.front==1) {y+=dis;} else if(td.front==2) {z+=dis;} else if(td.front==3) {x-=dis;} else if(td.front==4) {y-=dis;} else {z-=dis;} } printf("%d %d %d %d\n",x,y,z,td.front ); } return 0; }