欢迎使用CSDN-markdown编辑器

来源:互联网 发布:淘宝网天猫女装棉衣 编辑:程序博客网 时间:2024/06/03 15:21

20171005 模拟题 宇航员

题面

问题描述:
  宇航员在太空中迷失了方向,在他的起始位置现在建立一个虚拟xyz坐标系,称为绝对坐标系,宇航员正面的方向为x轴正方向,头顶方向为z轴正方向,则宇航员的初始状态如下图所示:这里写图片描述

现对六个方向分别标号,x,y,z正方向分别为0,1,2,负方向分别为3,4,5;称它们为绝对方向。宇航员在宇宙中只沿着与绝对坐标系xyz轴平行的方向行走,但是他不知道自己当前绝对坐标和自己面向的绝对方向。

任务描述:
  请根据宇航员对自己在相对方向上移动的描述确定宇航员最终的绝对坐标和面向的绝对方向。对在相对方向上移动的描述及意义如下:
forward x  向前走x米。
back x 先转向后,再走x米。
left x 先转向左,再走x米。
right x 先转向右,再走x米。
up x 先面向上,再走x米。
down x 先面向下,再走x米。
其中向上和向下如下图所示: 这里写图片描述

思路:因为小人处于不同地方时,小人的左右是不确定的,所以定义六个方向向量,把小人头的朝向和脸的朝向用向量表示,用向量叉乘来计算小人的左右。

include< algorithm>

include< cstdio>

include< cstring>

include< iostream>

include< string>

using namespace std;
struct note{
int xx;
int yy;
int zz;
};
note note_x_int(note a,int b){ //定义向量的数乘
a.xx=a.xx*b;
a.yy=a.yy*b;
a.zz=a.zz*b;
return a;
}
note note_x_note(note a,note b){ //定义响亮的叉乘
note c;
c.xx=a.yy*b.zz-a.zz*b.yy;
c.yy=b.xx*a.zz-b.zz*a.xx;
c.zz=a.xx*b.yy-a.yy*b.xx;
return c;
}
int found(note a){
if(a.xx==1) return 0;
if(a.xx==-1) return 3;
if(a.yy==1) return 1;
if(a.yy==-1) return 4;
if(a.zz==1) return 2;
if(a.zz==-1) return 5;

}
int main(){
int n;
scanf(“%d”,&n);
for(int i=1; i<=n; i++){
int m;
scanf(“%d”,&m);
note face,head;
face.xx=1; face.yy=0; face.zz=0;
head.xx=0; head.yy=0; head.zz=1;
int x=0,y=0,z=0;
for(int j=1; j<=m; j++){
string str; int len;
note t;
cin>>str>>len;
if(str[0]==’f’){
x=x+len*face.xx;
y=y+len*face.yy;
z=z+len*face.zz;
continue;
}
if(str[0]==’b’){
t=note_x_int(face,-1);
face=t;
x=x+len*face.xx;
y=y+len*face.yy;
z=z+len*face.zz;
continue;
}
if(str[0]==’u’){
t=face;
face=head;
head=note_x_int(t,-1);
x=x+len*face.xx;
y=y+len*face.yy;
z=z+len*face.zz;
continue;
}
if(str[0]==’d’){
t=head;
head=face;
face=note_x_int(t,-1);
x=x+len*face.xx;
y=y+len*face.yy;
z=z+len*face.zz;
continue;
}
if(str[0]==’l’){
t=note_x_note(face,head);
face=t;
x=x+len*face.xx;
y=y+len*face.yy;
z=z+len*face.zz;
continue;
}
if(str[0]==’r’){
t=note_x_note(head,face);
face=t;
x=x+len*face.xx;
y=y+len*face.yy;
z=z+len*face.zz;
continue;
}

    }    int k=found(face);    printf("%d %d %d %d\n",x,y,z,k);}return 0;

}

原创粉丝点击